Skip to content

Cannot find module with path that is URL-encoded #60771

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
mizdra opened this issue Dec 16, 2024 · 2 comments
Closed

Cannot find module with path that is URL-encoded #60771

mizdra opened this issue Dec 16, 2024 · 2 comments

Comments

@mizdra
Copy link

mizdra commented Dec 16, 2024

πŸ”Ž Search Terms

"import specifier", "resolution", "URL-encoded"

πŸ•— Version & Regression Information

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about Common "Bugs" That Aren't Bugs

⏯ Playground Link

No response

πŸ’» Code

https://stackblitz.com/edit/stackblitz-starters-xjsggz7l?file=src%2Findex.ts&view=editor

import value1 from './~.js';
import value2 from './%7E.js'; // Cannot find module or its corresponding type declarations.
import value3 from './|.js';
import value4 from './%7C.js'; // Cannot find module or its corresponding type declarations.

console.log(value1);
console.log(value2);
console.log(value3);
console.log(value4);

How to reproduce:

$ npm i
$ npm start
...

πŸ™ Actual behavior

I get a compile errors that the module cannot be found.

> start
> run-s -c tsc:*


> tsc:bundler
> tsc -p tsconfig.bundler.json --noEmit

error TS5083: Cannot read file '/home/projects/tsconfig.base.json'.

src/index.ts:2:20 - error TS2307: Cannot find module './%7E.js' or its corresponding type declarations.

2 import value2 from './%7E.js'; // Cannot find module or its corresponding type declarations.
                     ~~~~~~~~~~

src/index.ts:4:20 - error TS2307: Cannot find module './%7C.js' or its corresponding type declarations.

4 import value4 from './%7C.js'; // Cannot find module or its corresponding type declarations.
                     ~~~~~~~~~~


Found 3 errors in the same file, starting at: src/index.ts:2


> tsc:node
> tsc -p tsconfig.node.json --noEmit

error TS5083: Cannot read file '/home/projects/tsconfig.base.json'.

src/index.ts:2:20 - error TS2307: Cannot find module './%7E.js' or its corresponding type declarations.

2 import value2 from './%7E.js'; // Cannot find module or its corresponding type declarations.
                     ~~~~~~~~~~

src/index.ts:4:20 - error TS2307: Cannot find module './%7C.js' or its corresponding type declarations.

4 import value4 from './%7C.js'; // Cannot find module or its corresponding type declarations.
                     ~~~~~~~~~~


Found 3 errors in the same file, starting at: src/index.ts:2

ERROR: "tsc:node" exited with 2.
npm ERR! code EIO
npm ERR! syscall write
npm ERR! errno -5
npm ERR! EIO: i/o error, write

npm ERR! A complete log of this run can be found in: /home/.npm/_logs/2024-12-16T11_30_12_486Z-debug-0.log
jsh: spawn npm EIO

πŸ™‚ Expected behavior

No compilation errors.

Additional information about the issue

The resolution algorithm for import specifier is defined at the JavaScript runtime, not in ECMAScript. In Node.js and browsers, ~.mjs can be imported by import xxx from './%7E.mjs'. This is based on the following specifications:

https://nodejs.org/docs/v23.4.0/api/esm.html#urls

ES modules are resolved and cached as URLs. This means that special characters must be percent-encoded, such as # with %23 and ? with %3F.

https://html.spec.whatwg.org/multipage/webappapis.html#the-resolution-algorithm

The resolve a module specifier algorithm is the primary entry point for converting module specifier strings into URLs. When no import maps are involved, it is relatively straightforward, and reduces to resolving a URL-like module specifier.

I don't think URL-encoded is supported by all runtimes, but it seems to be common. This information will help you decide if you want TypeScript to support URL-encoded paths.

@MartinJohns
Copy link
Contributor

Duplicate of #55220 / #41730. Used search terms: import url in:title

@mizdra
Copy link
Author

mizdra commented Dec 16, 2024

Oh, sorry. I close this issue.

@mizdra mizdra closed this as completed Dec 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants