Skip to content

Commit

Permalink
module: better error for named exports from cjs
Browse files Browse the repository at this point in the history
We do not support importing named exports from a CJS module.
This change decorates the error message for missing named exports in
the case where the module being imported is expected to be CJS by the
ESM loader.

Signed-off-by: Myles Borins <[email protected]>

PR-URL: #33256
Reviewed-By: Anna Henningsen <[email protected]>
Reviewed-By: Michaël Zasso <[email protected]>
  • Loading branch information
MylesBorins authored and codebytere committed May 11, 2020
1 parent 0cbee57 commit ad86807
Show file tree
Hide file tree
Showing 15 changed files with 121 additions and 0 deletions.
27 changes: 27 additions & 0 deletions lib/internal/modules/esm/module_job.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
'use strict';

const {
ArrayPrototypeJoin,
ObjectSetPrototypeOf,
PromiseAll,
SafeSet,
SafePromise,
StringPrototypeIncludes,
StringPrototypeMatch,
StringPrototypeSplit,
} = primordials;

const { ModuleWrap } = internalBinding('module_wrap');
Expand Down Expand Up @@ -93,6 +97,29 @@ class ModuleJob {
}
} catch (e) {
decorateErrorStack(e);
if (StringPrototypeIncludes(e.message,
' does not provide an export named')) {
const splitStack = StringPrototypeSplit(e.stack, '\n');
const parentFileUrl = splitStack[0];
const childSpecifier = StringPrototypeMatch(e.message, /module '(.*)' does/)[1];
const childFileURL =
await this.loader.resolve(childSpecifier, parentFileUrl);
const format = await this.loader.getFormat(childFileURL);
if (format === 'commonjs') {
const importStatement = splitStack[1];
const namedImports = StringPrototypeMatch(importStatement, /{.*}/)[0];
e.message = `The requested module '${childSpecifier}' is expected ` +
'to be of type CommonJS, which does not support named exports. ' +
'CommonJS modules can be imported by importing the default ' +
'export.\n' +
'For example:\n' +
`import pkg from '${childSpecifier}';\n` +
`const ${namedImports} = pkg;`;
const newStack = StringPrototypeSplit(e.stack, '\n');
newStack[3] = `SyntaxError: ${e.message}`;
e.stack = ArrayPrototypeJoin(newStack, '\n');
}
}
throw e;
}
for (const dependencyJob of jobsInGraph) {
Expand Down
64 changes: 64 additions & 0 deletions test/es-module/test-esm-cjs-named-error.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import '../common/index.mjs';
import { rejects } from 'assert';

const fixtureBase = '../fixtures/es-modules/package-cjs-named-error';

const expectedRelative = 'The requested module \'./fail.cjs\' is expected to ' +
'be of type CommonJS, which does not support named exports. CommonJS ' +
'modules can be imported by importing the default export.\n' +
'For example:\n' +
'import pkg from \'./fail.cjs\';\n' +
'const { comeOn } = pkg;';

const expectedPackageHack = 'The requested module \'./json-hack/fail.js\' is ' +
'expected to be of type CommonJS, which does not support named exports. ' +
'CommonJS modules can be imported by importing the default export.\n' +
'For example:\n' +
'import pkg from \'./json-hack/fail.js\';\n' +
'const { comeOn } = pkg;';

const expectedBare = 'The requested module \'deep-fail\' is expected to ' +
'be of type CommonJS, which does not support named exports. CommonJS ' +
'modules can be imported by importing the default export.\n' +
'For example:\n' +
'import pkg from \'deep-fail\';\n' +
'const { comeOn } = pkg;';

rejects(async () => {
await import(`${fixtureBase}/single-quote.mjs`);
}, {
name: 'SyntaxError',
message: expectedRelative
}, 'should support relative specifiers with single quotes');

rejects(async () => {
await import(`${fixtureBase}/double-quote.mjs`);
}, {
name: 'SyntaxError',
message: expectedRelative
}, 'should support relative specifiers with double quotes');

rejects(async () => {
await import(`${fixtureBase}/json-hack.mjs`);
}, {
name: 'SyntaxError',
message: expectedPackageHack
}, 'should respect recursive package.json for module type');

rejects(async () => {
await import(`${fixtureBase}/bare-import-single.mjs`);
}, {
name: 'SyntaxError',
message: expectedBare
}, 'should support bare specifiers with single quotes');

rejects(async () => {
await import(`${fixtureBase}/bare-import-double.mjs`);
}, {
name: 'SyntaxError',
message: expectedBare
}, 'should support bare specifiers with double quotes');

rejects(async () => {
await import(`${fixtureBase}/escaped-single-quote.mjs`);
}, /import pkg from '\.\/oh'no\.cjs'/, 'should support relative specifiers with escaped single quote');
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import { comeOn } from "deep-fail";
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import { comeOn } from 'deep-fail';
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import { comeOn } from "./fail.cjs";
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import { value } from "./oh'no.cjs";
3 changes: 3 additions & 0 deletions test/fixtures/es-modules/package-cjs-named-error/fail.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
comeOn: 'fhqwhgads'
};
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import { comeOn } from './json-hack/fail.js';
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
comeOn: 'fhqwhgads'
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"type": "commonjs"
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions test/fixtures/es-modules/package-cjs-named-error/oh'no.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
value: 123
};
5 changes: 5 additions & 0 deletions test/fixtures/es-modules/package-cjs-named-error/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"name": "package-cjs-named-error",
"main": "index.mjs",
"type": "module"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import { comeOn } from './fail.cjs';

0 comments on commit ad86807

Please sign in to comment.