-
Notifications
You must be signed in to change notification settings - Fork 29.8k
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
Node does not determine that a file is an ES module when the package.json is in a long path #54304
Comments
# repro.sh
prefix=$(printf 'a%.0s' {1..128})
# Remove directory if it exists, then recreate it
rm -rf "$prefix" && mkdir -p "$prefix/$prefix"
# Create index.js and package.json in the nested directory
echo "export 'Loaded!';" > "$prefix/$prefix/index.js"
echo '{"type":"module"}' > "$prefix/$prefix/package.json"
# Run the Node.js script to import the module and log the output
node -p "import('./$prefix/$prefix/index.js').then(console.log)" $ bash repro.sh
Promise {
<rejected> SyntaxError: Unexpected token 'export'
at compileSourceTextModule (node:internal/modules/esm/utils:337:16)
at ModuleLoader.moduleStrategy (node:internal/modules/esm/translators:166:18)
at callTranslator (node:internal/modules/esm/loader:436:14)
at ModuleLoader.moduleProvider (node:internal/modules/esm/loader:442:30)
at async ModuleJob._link (node:internal/modules/esm/module_job:106:19)
}
file:///<...>/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/index.js:1
export 'Loaded!';
^^^^^^
SyntaxError: Unexpected token 'export'
at compileSourceTextModule (node:internal/modules/esm/utils:337:16)
at ModuleLoader.moduleStrategy (node:internal/modules/esm/translators:166:18)
at callTranslator (node:internal/modules/esm/loader:436:14)
at ModuleLoader.moduleProvider (node:internal/modules/esm/loader:442:30)
at async ModuleJob._link (node:internal/modules/esm/module_job:106:19)
Node.js v22.6.0 |
We probably miss ToNamespacedPath call somewhere |
First time contributing to node, or any C++ project I've tried to fix this, and here's what I found:
The function uses the URL parser to go to the parent path for some reason, and there is a comment saying it should not do that I rewrote the function to use std::filesystem::path instead of the URL parser and it seems to work |
Fixed in latest 22.9 |
Version
22.6.0
Platform
Subsystem
No response
What steps will reproduce the bug?
Create a .js file with the following code:
and a package.json file with the following code:
in a directory with path length greater than 260 characters
How often does it reproduce? Is there a required condition?
Always
What is the expected behavior? Why is that the expected behavior?
Node should look at the closest package.json, see "type": "module", determine that the file is an ES module, and run it
What do you see instead?
Node is unable to read the package.json, defaults to CommonJS, and fails at
export {}
Additional information
Showcase
The text was updated successfully, but these errors were encountered: