Skip to content

Commit 84a5e6c

Browse files
fabiosantoscodecodebytere
authored andcommitted
module: fix error message about importing names from cjs
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]>
1 parent 122d2b5 commit 84a5e6c

File tree

3 files changed

+18
-1
lines changed

3 files changed

+18
-1
lines changed

lib/internal/modules/esm/module_job.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const {
88
SafePromise,
99
StringPrototypeIncludes,
1010
StringPrototypeMatch,
11+
StringPrototypeReplace,
1112
StringPrototypeSplit,
1213
} = primordials;
1314

@@ -109,13 +110,14 @@ class ModuleJob {
109110
if (format === 'commonjs') {
110111
const importStatement = splitStack[1];
111112
const namedImports = StringPrototypeMatch(importStatement, /{.*}/)[0];
113+
const destructuringAssignment = StringPrototypeReplace(namedImports, /\s+as\s+/g, ': ');
112114
e.message = `The requested module '${childSpecifier}' is expected ` +
113115
'to be of type CommonJS, which does not support named exports. ' +
114116
'CommonJS modules can be imported by importing the default ' +
115117
'export.\n' +
116118
'For example:\n' +
117119
`import pkg from '${childSpecifier}';\n` +
118-
`const ${namedImports} = pkg;`;
120+
`const ${destructuringAssignment} = pkg;`;
119121
const newStack = StringPrototypeSplit(e.stack, '\n');
120122
newStack[3] = `SyntaxError: ${e.message}`;
121123
e.stack = ArrayPrototypeJoin(newStack, '\n');

test/es-module/test-esm-cjs-named-error.mjs

+14
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,13 @@ const expectedRelative = 'The requested module \'./fail.cjs\' is expected to ' +
1010
'import pkg from \'./fail.cjs\';\n' +
1111
'const { comeOn } = pkg;';
1212

13+
const expectedRenamed = 'The requested module \'./fail.cjs\' is expected to ' +
14+
'be of type CommonJS, which does not support named exports. CommonJS ' +
15+
'modules can be imported by importing the default export.\n' +
16+
'For example:\n' +
17+
'import pkg from \'./fail.cjs\';\n' +
18+
'const { comeOn: comeOnRenamed } = pkg;';
19+
1320
const expectedPackageHack = 'The requested module \'./json-hack/fail.js\' is ' +
1421
'expected to be of type CommonJS, which does not support named exports. ' +
1522
'CommonJS modules can be imported by importing the default export.\n' +
@@ -38,6 +45,13 @@ rejects(async () => {
3845
message: expectedRelative
3946
}, 'should support relative specifiers with double quotes');
4047

48+
rejects(async () => {
49+
await import(`${fixtureBase}/renamed-import.mjs`);
50+
}, {
51+
name: 'SyntaxError',
52+
message: expectedRenamed
53+
}, 'should correctly format named imports with renames');
54+
4155
rejects(async () => {
4256
await import(`${fixtureBase}/json-hack.mjs`);
4357
}, {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import { comeOn as comeOnRenamed } from "./fail.cjs"

0 commit comments

Comments
 (0)