Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

module: fix regression in main ESM loading #15736

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 7 additions & 28 deletions lib/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -421,35 +421,14 @@ Module._load = function(request, parent, isMain) {
debug('Module._load REQUEST %s parent: %s', request, parent.id);
}

var filename = null;
var filename = Module._resolveFilename(request, parent, isMain);

if (isMain) {
let err;
try {
filename = Module._resolveFilename(request, parent, isMain);
} catch (e) {
// try to keep stack
e.stack;
err = e;
}
if (experimentalModules) {
if (filename === null || /\.mjs$/.test(filename)) {
try {
ESMLoader.import(getURLFromFilePath(filename).href).catch((e) => {
console.error(e);
process.exit(1);
});
return;
} catch (e) {
// well, it isn't ESM
}
}
}
if (err) {
throw err;
}
} else {
filename = Module._resolveFilename(request, parent, isMain);
if (isMain && experimentalModules && /\.mjs$/.test(filename)) {
ESMLoader.import(getURLFromFilePath(filename).href).catch((e) => {
console.error(e);
process.exit(1);
});
return;
}

var cachedModule = Module._cache[filename];
Expand Down
22 changes: 22 additions & 0 deletions test/parallel/test-module-main-fail.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
'use strict';
const common = require('../common');
const assert = require('assert');
const { execFileSync } = require('child_process');

const entryPoints = ['iDoNotExist', 'iDoNotExist.js', 'iDoNotExist.mjs'];
const flags = [[], ['--experimental-modules']];
const node = process.argv[0];

for (const args of flags) {
for (const entryPoint of entryPoints) {
try {
execFileSync(node, args.concat(entryPoint), { stdio: 'pipe' });
} catch (e) {
assert.strictEqual(e.stdout.toString(), '');
assert(e.stderr.toString().match(/Error: Cannot find module/));
continue;
}
assert.fail('Executing node with inexistent entry point should fail. ' +
`Entry point: ${entryPoint}, Flags: [${args}]`);
}
}