Repository navigation
Unsupported character(s) in import module-name #41052
Description
Activity
import specifiers are URLs. you'll need to replace
#with%23and\with%5CReacted by notroid5- addedesmIssues and PRs related to the ECMAScript Modules implementation.Issues and PRs related to the ECMAScript Modules implementation.
on Dec 2, 2021 Okay, that works but is this documented somewhere?
And even if, import works with local paths, so it is expected to replace problematic characters by itself and not by the user.
Which it clearly does not, ergo this issue.Okay, that works but is this documented somewhere?
Yes. https://nodejs.org/api/esm.html#urls
ES modules are resolved and cached as URLs. This means that files containing special characters such as
#and?need to be escaped.There is probably room for improvement. For example, it could indicate the type of escaping, as escaping with
\won't work.If you or someone else reading this would like to suggest an improvement, you can click the "Edit On GitHub" link at the top of that page, or you can edit the
doc/api/esm.mdfile in this repository and submit the change as a pull request.And even if, import works with local paths, so it is expected to replace problematic characters by itself and not by the user.
There's probably a discussion to be had about whether this calls for a doc enhancement, a change in the error message, or an actual change in the behavior of the ESM system. (I would imagine that last one seems unlikely to happen, but I'm not deeply engaged in the discussions around that stuff so I could be very wrong.) (Also, those three options are not mutually exclusive.)
@nodejs/modules
import works with local paths
Well precisely it doesn't, hence your issue. If all the parts of your path only contains alphanumeric characters, and you are using an OS that accepts
/to as path separator, you may think that those URLs are paths, but it's just a coincidence. On Windows, you may have noticed you cannot use\to separate folders in the specifiers, no matter how you try to escape those, because it's not a path.So you are trying to specify a relative URL, the problem is that
#is a special character for URLs:$ new URL('./#user/user0.js', 'file:///local/cwd/') URL { hash: "#user/user0.js" host: "" hostname: "" href: "file:///local/cwd/#user/user0.js" origin: "file://" password: "" pathname: "/local/cwd/" port: "" protocol: "file:" search: "" searchParams: URLSearchParams {} username: "" [[Prototype]]: URL }
The same thing would appear if you were using
?in your directory name (although that's not a problem you can have on Windows, as it forbids use of?in file names for some reason).- added a commit that references this issue
on Dec 7, 2021 - added 2 commits that reference this issue
on Jan 31, 2022 - added a commit that references this issue
on Feb 1, 2022
Version
16.13.0
Platform
Microsoft Windows NT 10.0.19043.0 x64
Subsystem
No response
What steps will reproduce the bug?
server.js:package.json:#(probably not the only unsupported character), e.g.#useruser0.js:How often does it reproduce? Is there a required condition?
Node.js always throws an unconditional error during the import.
What is the expected behavior?
It should log the string.
What do you see instead?
Additional information
npm -v: 8.1.0\the#does not work#, it works as expected#, yet is aware of the rest of the full path...?import module-names, so I have to assume there is none, which would mean that every character should be supported, especially since it's just a string pointing to a fileEdit: Typo