@@ -44,6 +44,7 @@ function resolveMainPath(main) {
44
44
} catch ( err ) {
45
45
if ( defaultType === 'module' && err ?. code === 'ENOENT' ) {
46
46
const { decorateErrorWithCommonJSHints } = require ( 'internal/modules/esm/resolve' ) ;
47
+ const { getCWDURL } = require ( 'internal/util' ) ;
47
48
decorateErrorWithCommonJSHints ( err , mainPath , getCWDURL ( ) ) ;
48
49
}
49
50
throw err ;
@@ -85,10 +86,6 @@ function shouldUseESMLoader(mainPath) {
85
86
case 'commonjs' :
86
87
return false ;
87
88
default : { // No package.json or no `type` field.
88
- if ( getOptionValue ( '--experimental-detect-module' ) ) {
89
- // If the first argument of `containsModuleSyntax` is undefined, it will read `mainPath` from the file system.
90
- return containsModuleSyntax ( undefined , mainPath ) ;
91
- }
92
89
return false ;
93
90
}
94
91
}
@@ -153,12 +150,43 @@ function runEntryPointWithESMLoader(callback) {
153
150
* by `require('module')`) even when the entry point is ESM.
154
151
* This monkey-patchable code is bypassed under `--experimental-default-type=module`.
155
152
* Because of backwards compatibility, this function is exposed publicly via `import { runMain } from 'node:module'`.
153
+ * When `--experimental-detect-module` is passed, this function will attempt to run ambiguous (no explicit extension, no
154
+ * `package.json` type field) entry points as CommonJS first; under certain conditions, it will retry running as ESM.
156
155
* @param {string } main - First positional CLI argument, such as `'entry.js'` from `node entry.js`
157
156
*/
158
157
function executeUserEntryPoint ( main = process . argv [ 1 ] ) {
159
158
const resolvedMain = resolveMainPath ( main ) ;
160
159
const useESMLoader = shouldUseESMLoader ( resolvedMain ) ;
161
- if ( useESMLoader ) {
160
+
161
+ // Unless we know we should use the ESM loader to handle the entry point per the checks in `shouldUseESMLoader`, first
162
+ // try to run the entry point via the CommonJS loader; and if that fails under certain conditions, retry as ESM.
163
+ let retryAsESM = false ;
164
+ if ( ! useESMLoader ) {
165
+ const cjsLoader = require ( 'internal/modules/cjs/loader' ) ;
166
+ const { Module } = cjsLoader ;
167
+ if ( getOptionValue ( '--experimental-detect-module' ) ) {
168
+ try {
169
+ // Module._load is the monkey-patchable CJS module loader.
170
+ Module . _load ( main , null , true ) ;
171
+ } catch ( error ) {
172
+ const source = cjsLoader . entryPointSource ;
173
+ const { shouldRetryAsESM } = require ( 'internal/modules/helpers' ) ;
174
+ retryAsESM = shouldRetryAsESM ( error . message , source ) ;
175
+ // In case the entry point is a large file, such as a bundle,
176
+ // ensure no further references can prevent it being garbage-collected.
177
+ cjsLoader . entryPointSource = undefined ;
178
+ if ( ! retryAsESM ) {
179
+ const { enrichCJSError } = require ( 'internal/modules/esm/translators' ) ;
180
+ enrichCJSError ( error , source , resolvedMain ) ;
181
+ throw error ;
182
+ }
183
+ }
184
+ } else { // `--experimental-detect-module` is not passed
185
+ Module . _load ( main , null , true ) ;
186
+ }
187
+ }
188
+
189
+ if ( useESMLoader || retryAsESM ) {
162
190
const mainPath = resolvedMain || main ;
163
191
const mainURL = pathToFileURL ( mainPath ) . href ;
164
192
@@ -167,10 +195,6 @@ function executeUserEntryPoint(main = process.argv[1]) {
167
195
// even after the event loop stops running.
168
196
return cascadedLoader . import ( mainURL , undefined , { __proto__ : null } , true ) ;
169
197
} ) ;
170
- } else {
171
- // Module._load is the monkey-patchable CJS module loader.
172
- const { Module } = require ( 'internal/modules/cjs/loader' ) ;
173
- Module . _load ( main , null , true ) ;
174
198
}
175
199
}
176
200
0 commit comments