You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
With "type": "commonjs", #!/usr/bin/env node file does not execute but returns success and no error messages when the file is ESM #61104
Now add to your package.json the line "type": "commonjs". Run the script again, and note that it produces no output at all (neither what it is expected to print nor any errors), and yet still exits with exit code 0, indicating success:
If you feel like it, you can replace the ESM import with const { version } = require('node:process') and see that it does work as in the first example run above.
How often does it reproduce? Is there a required condition?
Always reproduces.
What is the expected behavior? Why is that the expected behavior?
I expect the "type": "commonjs" case to do two things, in order of importance:
Exit with a non-zero exit code (indicating "failure") because it didn't run. This would help avoid the especially insidious situation where it's a script running in a build system that's expected to do something important but produce no output. (Ask me how I know this is "insidious." :-))
Print some sort of error to stderr indicating that the script can't be run, ideally including some information about the configuration that's preventing this. (In this case, I guess, it's that the auto-detection of ESM vs. CommonJS based on file contents is disabled due to the "type": "commonjs" in package.json?)
What do you see instead?
See output in reproduction above.
Additional information
node --print-all-exceptions ./script >exceptions.txt 2>&1 may have some useful information in its 1240 lines. I've attached the output from my machine as exceptions.txt.
Also, possibly #49444 "Feature: ESM in executable files" may have some relation to this, though I've not read through it carefully (it's a long read).
changed the title [-]`#!/usr/bin/env node` file does not execute but returns success and no error messages when the file is ESM[/-][+]With `"type": "commonjs"`, `#!/usr/bin/env node` file does not execute but returns success and no error messages when the file is ESM[/+]on Dec 18, 2025
Activity
[-]`#!/usr/bin/env node` file does not execute but returns success and no error messages when the file is ESM[/-][+]With `"type": "commonjs"`, `#!/usr/bin/env node` file does not execute but returns success and no error messages when the file is ESM[/+]aduh95 commentedon Dec 18, 2025
I'm able to reproduce on
mainand up to 20.19.6 with the following:The output is
aduh95 commentedon Dec 18, 2025
/cc @nodejs/loaders
module: fix silent failure for ESM syntax in explicit commonjs entry
esm: detect ESM syntax in extensionless files under type:commonjs
module: fix extensionless entry with explicit type=commonjs
module: fix extensionless entry with explicit type=commonjs
module: fix extensionless CJS files in type:module packages
module: fix extensionless CJS files in `"type": "module"` packages
kjay46857-cmyk commentedon Mar 19, 2026
6f4a4f6- [ ] ef4c3