Skip to content

Commit

Permalink
module: fix error message about importing names from cjs
Browse files Browse the repository at this point in the history
When importing specific names from a CJS module, and renaming them using
`as`, the example fix in the error message erroneously contains the
keyword `as` in the destructuring variable declaration.

Example of this issue:

    import { parse as acornParse } from "acorn";
             ^^^^^
    SyntaxError: The requested module 'acorn' is expected to be of type CommonJS, which does not support named exports. CommonJS modules can be imported by importing the default export.
    For example:
    import pkg from 'acorn';
    const { parse as acornParse } = pkg;

PR-URL: #33882
Reviewed-By: Anna Henningsen <[email protected]>
Reviewed-By: Michaël Zasso <[email protected]>
Reviewed-By: Guy Bedford <[email protected]>
Reviewed-By: Myles Borins <[email protected]>
  • Loading branch information
fabiosantoscode authored and codebytere committed Jul 14, 2020
1 parent 13d1cae commit 42ebe7a
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
4 changes: 3 additions & 1 deletion lib/internal/modules/esm/module_job.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const {
SafePromise,
StringPrototypeIncludes,
StringPrototypeMatch,
StringPrototypeReplace,
StringPrototypeSplit,
} = primordials;

Expand Down Expand Up @@ -108,13 +109,14 @@ class ModuleJob {
if (format === 'commonjs') {
const importStatement = splitStack[1];
const namedImports = StringPrototypeMatch(importStatement, /{.*}/)[0];
const destructuringAssignment = StringPrototypeReplace(namedImports, /\s+as\s+/g, ': ');
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 ${destructuringAssignment} = pkg;`;
const newStack = StringPrototypeSplit(e.stack, '\n');
newStack[3] = `SyntaxError: ${e.message}`;
e.stack = ArrayPrototypeJoin(newStack, '\n');
Expand Down
14 changes: 14 additions & 0 deletions test/es-module/test-esm-cjs-named-error.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@ const expectedRelative = 'The requested module \'./fail.cjs\' is expected to ' +
'import pkg from \'./fail.cjs\';\n' +
'const { comeOn } = pkg;';

const expectedRenamed = '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: comeOnRenamed } = 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' +
Expand Down Expand Up @@ -38,6 +45,13 @@ rejects(async () => {
message: expectedRelative
}, 'should support relative specifiers with double quotes');

rejects(async () => {
await import(`${fixtureBase}/renamed-import.mjs`);
}, {
name: 'SyntaxError',
message: expectedRenamed
}, 'should correctly format named imports with renames');

rejects(async () => {
await import(`${fixtureBase}/json-hack.mjs`);
}, {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import { comeOn as comeOnRenamed } from "./fail.cjs"

0 comments on commit 42ebe7a

Please sign in to comment.