Skip to content

Commit 527c727

Browse files
committed
module: fix regression in main ESM loading
When the requested module cannot be resolved to a file, loading should always fail, regardless of wether ESM is enabled or not. Fixes: #15732
1 parent 4f339b5 commit 527c727

2 files changed

Lines changed: 35 additions & 28 deletions

File tree

lib/module.js

Lines changed: 7 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -421,35 +421,14 @@ Module._load = function(request, parent, isMain) {
421421
debug('Module._load REQUEST %s parent: %s', request, parent.id);
422422
}
423423

424-
var filename = null;
424+
var filename = Module._resolveFilename(request, parent, isMain);
425425

426-
if (isMain) {
427-
let err;
428-
try {
429-
filename = Module._resolveFilename(request, parent, isMain);
430-
} catch (e) {
431-
// try to keep stack
432-
e.stack;
433-
err = e;
434-
}
435-
if (experimentalModules) {
436-
if (filename === null || /\.mjs$/.test(filename)) {
437-
try {
438-
ESMLoader.import(getURLFromFilePath(filename).href).catch((e) => {
439-
console.error(e);
440-
process.exit(1);
441-
});
442-
return;
443-
} catch (e) {
444-
// well, it isn't ESM
445-
}
446-
}
447-
}
448-
if (err) {
449-
throw err;
450-
}
451-
} else {
452-
filename = Module._resolveFilename(request, parent, isMain);
426+
if (isMain && experimentalModules && /\.mjs$/.test(filename)) {
427+
ESMLoader.import(getURLFromFilePath(filename).href).catch((e) => {
428+
console.error(e);
429+
process.exit(1);
430+
});
431+
return;
453432
}
454433

455434
var cachedModule = Module._cache[filename];
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
'use strict';
2+
const common = require('../common');
3+
const assert = require('assert');
4+
const child_process = require('child_process');
5+
const { promisify } = require('util');
6+
const execFile = promisify(child_process.execFile);
7+
8+
common.crashOnUnhandledRejection();
9+
10+
const entryPoints = ['iDoNotExist', 'iDoNotExist.js', 'iDoNotExist.mjs'];
11+
const flags = [[], ['--experimental-modules']];
12+
const node = process.argv[0];
13+
14+
(async () => {
15+
for (const args of flags) {
16+
for (const entryPoint of entryPoints) {
17+
try {
18+
await execFile(node, args.concat(entryPoint));
19+
} catch (e) {
20+
assert.strictEqual(e.stdout, '');
21+
assert(e.stderr.match(/Error: Cannot find module/));
22+
continue;
23+
}
24+
throw new Error('Executing node with inexistent entry point should ' +
25+
`fail. Entry point: ${entryPoint}, Flags: [${args}]`);
26+
}
27+
}
28+
})();

0 commit comments

Comments
 (0)