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

ESM swallowing MODULE_NOT_FOUND errors in case of type:module #4687

Merged
merged 2 commits into from Jul 13, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 15 additions & 1 deletion lib/esm-utils.js
Expand Up @@ -52,7 +52,21 @@ exports.requireOrImport = hasStableEsmImplementation
err.code === 'ERR_UNKNOWN_FILE_EXTENSION' ||
err.code === 'ERR_UNSUPPORTED_DIR_IMPORT'
) {
return require(file);
try {
return require(file);
} catch (requireErr) {
if (requireErr.code === 'ERR_REQUIRE_ESM') {
// This happens when the test file is a JS file, but via type:module is actually ESM,
// AND has an import to a file that doesn't exist.
// This throws an `ERR_MODULE_NOT_FOUND` // error above,
// and when we try to `require` it here, it throws an `ERR_REQUIRE_ESM`.
// What we want to do is throw the original error (the `ERR_MODULE_NOT_FOUND`),
// and not the `ERR_REQUIRE_ESM` error, which is a red herring.
throw err;
} else {
throw requireErr;
}
}
} else {
throw err;
}
Expand Down
27 changes: 24 additions & 3 deletions test/integration/esm.spec.js
@@ -1,8 +1,11 @@
'use strict';
var path = require('path');
var helpers = require('./helpers');
var run = helpers.runMochaJSON;
var runMochaAsync = helpers.runMochaAsync;
const {
runMochaJSON: run,
runMochaAsync,
invokeMochaAsync,
resolveFixturePath
} = require('./helpers');
var utils = require('../../lib/utils');
var args =
+process.versions.node.split('.')[0] >= 13 ? [] : ['--experimental-modules'];
Expand Down Expand Up @@ -81,4 +84,22 @@ describe('esm', function() {

expect(result, 'to have passed test count', 1);
});

it('should throw an ERR_MODULE_NOT_FOUND and not ERR_REQUIRE_ESM if file imports a non-existing module', async function() {
const fixture =
'esm/type-module/test-that-imports-non-existing-module.fixture.js';

const [, promise] = invokeMochaAsync(
[resolveFixturePath(fixture), '--unhandled-rejections=warn'],
{stdio: 'pipe'}
);
juergba marked this conversation as resolved.
Show resolved Hide resolved
const result = await promise;

expect(result, 'to have failed with output', /ERR_MODULE_NOT_FOUND/);
expect(
result,
'to have failed with output',
/test-that-imports-non-existing-module/
);
});
});
3 changes: 3 additions & 0 deletions test/integration/fixtures/esm/type-module/package.json
@@ -0,0 +1,3 @@
{
"type": "module"
}
juergba marked this conversation as resolved.
Show resolved Hide resolved
@@ -0,0 +1,6 @@
import assert from 'assert';
import './this-module-does-not-exist.js';

it('should not pass (or even run) because above import will fail', () => {
assert(true);
});