Commit f4b05c0
esm: detect ESM syntax in extensionless files under type:commonjs
When an extensionless file (common for CLI scripts with shebangs)
contains ES module syntax but the nearest package.json has
"type": "commonjs", Node.js silently exits with code 0 and produces
no output or error. This happens because getFileProtocolModuleFormat()
returns 'commonjs' for extensionless files based solely on the package
type, without checking the file content for ESM syntax.
For extensionless files, when source is available, run
detectModuleFormat() before returning the package type. If the file
contains ES module syntax, return 'module' so it is loaded as ESM
rather than silently failing as CJS.
This is consistent with how the 'none' (no type field) case already
works for extensionless files, where detectModuleFormat() is called
at line 176.
Fixes: #61104
Co-authored-by: Cursor <cursoragent@cursor.com>1 parent b220fbe commit f4b05c0
File tree
2 files changed
+62
-0
lines changed- lib/internal/modules/esm
- test/parallel
2 files changed
+62
-0
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
162 | 162 | | |
163 | 163 | | |
164 | 164 | | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
165 | 175 | | |
166 | 176 | | |
167 | 177 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
0 commit comments