| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
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: nodejs#61104 Co-authored-by: Cursor <cursoragent@cursor.com>
|
Review requested:
|
Sorry, something went wrong.
|
This pull request has been marked as stale due to 90 days of inactivity. |
Sorry, something went wrong.
| Back | FazBrowse Home | New Git URL |
Summary
Fixes a silent failure where extensionless files containing ES module syntax produce no output and exit with code 0 when the nearest package.json has "type": "commonjs".
This is a common scenario for CLI tools that use shebangs (#!/usr/bin/env node) without a file extension.
Reproduction
Root Cause
In lib/internal/modules/esm/get_format.js, getFileProtocolModuleFormat() handles extensionless files (line 159-177). When packageType is 'commonjs', it returns 'commonjs' immediately without checking the file content for ESM syntax (line 164-165).
This causes the ESM loader to treat the file as a CJS module, routing it through createCJSModuleWrap in the translators, which wraps the ESM code as CJS — silently producing an empty/non-functional module.
Compare with:
Fix
For extensionless files when packageType !== 'none' (i.e., explicitly 'commonjs'), check the source content via detectModuleFormat() before returning the package type. If the file contains ES module syntax, return 'module' so it's loaded correctly.
This is consistent with how ambiguous files are already handled elsewhere in the same function, and relies on the existing containsModuleSyntax V8 binding (used by detectModuleFormat) which is already enabled by default via --experimental-detect-module.
Test
test/parallel/test-esm-extensionless-commonjs-type.js — creates an extensionless ESM file in a type: commonjs project, runs it, and asserts it does not silently exit with code 0.
Fixes: #61104
Made with Cursor