Description
π 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.