Skip to content

Commit 219e9fe

Browse files
ctavanMylesBorins
authored andcommitted
module: fix crash on multiline named cjs imports
The node process crashes when trying to parse a multiline import statement for named exports of a CommonJS module: TypeError: Cannot read property '0' of null at ModuleJob._instantiate (internal/modules/esm/module_job.js:112:77) at async ModuleJob.run (internal/modules/esm/module_job.js:137:5) at async Loader.import (internal/modules/esm/loader.js:165:24) at async rejects.name (file:///***/node/test/es-module/test-esm-cjs-named-error.mjs:56:3) at async waitForActual (assert.js:721:5) at async rejects (assert.js:830:25), The reason is that the regexp that is currently used to decorate the original error fails for multi line import statements. Unfortunately the undecorated error stack only contains the single line which causes the import to fail: file:///***/node/test/fixtures/es-modules/package-cjs-named-error/multi-line.mjs:2 comeOn, ^^^^^^ SyntaxError: The requested module './fail.cjs' does not provide an export named 'comeOn' at ModuleJob._instantiate (internal/modules/esm/module_job.js:98:21) at async ModuleJob.run (internal/modules/esm/module_job.js:141:5) at async Loader.import (internal/modules/esm/loader.js:165:24) at async rejects.name (file:///***/node/test/es-module/test-esm-cjs-named-error.mjs:56:3) at async waitForActual (assert.js:721:5) at async rejects (assert.js:830:25) Hence, for multiline import statements we cannot create an equivalent piece of code that uses default import followed by an object destructuring assignment. In any case the node process should definitely not crash. So until we have a more sophisticated way of extracting the entire problematic multiline import statement, show the code example only for single-line imports where the current regexp approach works well. Refs: #35259 PR-URL: #35275 Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Myles Borins <[email protected]> Reviewed-By: Rich Trott <[email protected]>
1 parent fb88257 commit 219e9fe

File tree

4 files changed

+31
-8
lines changed

4 files changed

+31
-8
lines changed

lib/internal/modules/esm/module_job.js

+14-7
Original file line numberDiff line numberDiff line change
@@ -108,16 +108,23 @@ class ModuleJob {
108108
await this.loader.resolve(childSpecifier, parentFileUrl);
109109
const format = await this.loader.getFormat(childFileURL);
110110
if (format === 'commonjs') {
111-
const importStatement = splitStack[1];
112-
const namedImports = StringPrototypeMatch(importStatement, /{.*}/)[0];
113-
const destructuringAssignment = StringPrototypeReplace(namedImports, /\s+as\s+/g, ': ');
114111
e.message = `The requested module '${childSpecifier}' is expected ` +
115112
'to be of type CommonJS, which does not support named exports. ' +
116113
'CommonJS modules can be imported by importing the default ' +
117-
'export.\n' +
118-
'For example:\n' +
119-
`import pkg from '${childSpecifier}';\n` +
120-
`const ${destructuringAssignment} = pkg;`;
114+
'export.';
115+
// TODO(@ctavan): The original error stack only provides the single
116+
// line which causes the error. For multi-line import statements we
117+
// cannot generate an equivalent object descructuring assignment by
118+
// just parsing the error stack.
119+
const importStatement = splitStack[1];
120+
const oneLineNamedImports = StringPrototypeMatch(importStatement, /{.*}/);
121+
if (oneLineNamedImports) {
122+
const destructuringAssignment =
123+
StringPrototypeReplace(oneLineNamedImports[0], /\s+as\s+/g, ': ');
124+
e.message += '\nFor example:\n' +
125+
`import pkg from '${childSpecifier}';\n` +
126+
`const ${destructuringAssignment} = pkg;`;
127+
}
121128
const newStack = StringPrototypeSplit(e.stack, '\n');
122129
newStack[3] = `SyntaxError: ${e.message}`;
123130
e.stack = ArrayPrototypeJoin(newStack, '\n');

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

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

13+
const expectedWithoutExample = 'The requested module \'./fail.cjs\' is ' +
14+
'expected to be of type CommonJS, which does not support named exports. ' +
15+
'CommonJS modules can be imported by importing the default export.';
16+
1317
const expectedRenamed = 'The requested module \'./fail.cjs\' is expected to ' +
1418
'be of type CommonJS, which does not support named exports. CommonJS ' +
1519
'modules can be imported by importing the default export.\n' +
@@ -52,6 +56,13 @@ rejects(async () => {
5256
message: expectedRenamed
5357
}, 'should correctly format named imports with renames');
5458

59+
rejects(async () => {
60+
await import(`${fixtureBase}/multi-line.mjs`);
61+
}, {
62+
name: 'SyntaxError',
63+
message: expectedWithoutExample,
64+
}, 'should correctly format named imports across multiple lines');
65+
5566
rejects(async () => {
5667
await import(`${fixtureBase}/json-hack.mjs`);
5768
}, {
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
module.exports = {
2-
comeOn: 'fhqwhgads'
2+
comeOn: 'fhqwhgads',
3+
everybody: 'to the limit',
34
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import {
2+
comeOn,
3+
everybody,
4+
} from './fail.cjs';

0 commit comments

Comments
 (0)