Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit f23a0e2

Browse filesBrowse files
guybedforddanielleadams
authored andcommitted
module: refine module type mismatch error cases
PR-URL: #35426 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Bradley Farias <bradley.meck@gmail.com> Reviewed-By: Myles Borins <myles.borins@gmail.com>
1 parent 7dc6b2f commit f23a0e2
Copy full SHA for f23a0e2

File tree

Expand file treeCollapse file tree

5 files changed

+55
-26
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

5 files changed

+55
-26
lines changed
Open diff view settings
Collapse file

‎lib/internal/modules/cjs/loader.js‎

Copy file name to clipboardExpand all lines: lib/internal/modules/cjs/loader.js
+1-23Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,6 @@ const fs = require('fs');
7070
const internalFS = require('internal/fs/utils');
7171
const path = require('path');
7272
const { sep } = path;
73-
const { emitWarningSync } = require('internal/process/warning');
7473
const { internalModuleStat } = internalBinding('fs');
7574
const packageJsonReader = require('internal/modules/package_json_reader');
7675
const { safeGetenv } = internalBinding('credentials');
@@ -115,6 +114,7 @@ const {
115114
} = require('internal/util/types');
116115

117116
const asyncESM = require('internal/process/esm_loader');
117+
const { enrichCJSError } = require('internal/modules/esm/translators');
118118
const { kEvaluated } = internalBinding('module_wrap');
119119
const {
120120
encodedSepRegEx,
@@ -129,28 +129,6 @@ const relativeResolveCache = ObjectCreate(null);
129129
let requireDepth = 0;
130130
let statCache = null;
131131

132-
function enrichCJSError(err) {
133-
const stack = err.stack.split('\n');
134-
135-
const lineWithErr = stack[1];
136-
137-
/*
138-
The regular expression below targets the most common import statement
139-
usage. However, some cases are not matching, cases like import statement
140-
after a comment block and/or after a variable definition.
141-
*/
142-
if (err.message.startsWith('Unexpected token \'export\'') ||
143-
(RegExpPrototypeTest(/^\s*import(?=[ {'"*])\s*(?![ (])/, lineWithErr))) {
144-
// Emit the warning synchronously because we are in the middle of handling
145-
// a SyntaxError that will throw and likely terminate the process before an
146-
// asynchronous warning would be emitted.
147-
emitWarningSync(
148-
'To load an ES module, set "type": "module" in the package.json or use ' +
149-
'the .mjs extension.'
150-
);
151-
}
152-
}
153-
154132
function stat(filename) {
155133
filename = path.toNamespacedPath(filename);
156134
if (statCache !== null) {
Collapse file

‎lib/internal/modules/esm/translators.js‎

Copy file name to clipboardExpand all lines: lib/internal/modules/esm/translators.js
+37-2Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,12 @@ const {
99
ObjectKeys,
1010
PromisePrototypeCatch,
1111
PromiseReject,
12+
RegExpPrototypeTest,
1213
SafeMap,
1314
SafeSet,
1415
StringPrototypeReplace,
16+
StringPrototypeSplit,
17+
StringPrototypeStartsWith,
1518
} = primordials;
1619

1720
let _TYPES = null;
@@ -54,9 +57,11 @@ const experimentalImportMetaResolve =
5457
getOptionValue('--experimental-import-meta-resolve');
5558
const asyncESM = require('internal/process/esm_loader');
5659
const cjsParse = require('internal/deps/cjs-module-lexer/lexer');
60+
const { emitWarningSync } = require('internal/process/warning');
5761

5862
const translators = new SafeMap();
5963
exports.translators = translators;
64+
exports.enrichCJSError = enrichCJSError;
6065

6166
let DECODER = null;
6267
function assertBufferSource(body, allowString, hookName) {
@@ -130,6 +135,25 @@ translators.set('module', async function moduleStrategy(url) {
130135
return module;
131136
});
132137

138+
function enrichCJSError(err) {
139+
const stack = StringPrototypeSplit(err.stack, '\n');
140+
/*
141+
* The regular expression below targets the most common import statement
142+
* usage. However, some cases are not matching, cases like import statement
143+
* after a comment block and/or after a variable definition.
144+
*/
145+
if (StringPrototypeStartsWith(err.message, 'Unexpected token \'export\'') ||
146+
RegExpPrototypeTest(/^\s*import(?=[ {'"*])\s*(?![ (])/, stack[1])) {
147+
// Emit the warning synchronously because we are in the middle of handling
148+
// a SyntaxError that will throw and likely terminate the process before an
149+
// asynchronous warning would be emitted.
150+
emitWarningSync(
151+
'To load an ES module, set "type": "module" in the package.json or use ' +
152+
'the .mjs extension.'
153+
);
154+
}
155+
}
156+
133157
// Strategy for loading a node-style CommonJS module
134158
const isWindows = process.platform === 'win32';
135159
const winSepRegEx = /\//g;
@@ -152,7 +176,12 @@ translators.set('commonjs', async function commonjsStrategy(url, isMain) {
152176
exports = asyncESM.ESMLoader.cjsCache.get(module);
153177
asyncESM.ESMLoader.cjsCache.delete(module);
154178
} else {
155-
exports = CJSModule._load(filename, undefined, isMain);
179+
try {
180+
exports = CJSModule._load(filename, undefined, isMain);
181+
} catch (err) {
182+
enrichCJSError(err);
183+
throw err;
184+
}
156185
}
157186

158187
for (const exportName of exportNames) {
@@ -190,7 +219,13 @@ function cjsPreparseModuleExports(filename) {
190219
source = readFileSync(filename, 'utf8');
191220
} catch {}
192221

193-
const { exports, reexports } = cjsParse(source || '');
222+
let exports, reexports;
223+
try {
224+
({ exports, reexports } = cjsParse(source || ''));
225+
} catch {
226+
exports = [];
227+
reexports = [];
228+
}
194229

195230
const exportNames = new SafeSet(exports);
196231

Collapse file

‎test/es-module/test-esm-cjs-exports.js‎

Copy file name to clipboardExpand all lines: test/es-module/test-esm-cjs-exports.js
+15-1Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const assert = require('assert');
77

88
const entry = fixtures.path('/es-modules/cjs-exports.mjs');
99

10-
const child = spawn(process.execPath, [entry]);
10+
let child = spawn(process.execPath, [entry]);
1111
child.stderr.setEncoding('utf8');
1212
let stdout = '';
1313
child.stdout.setEncoding('utf8');
@@ -19,3 +19,17 @@ child.on('close', common.mustCall((code, signal) => {
1919
assert.strictEqual(signal, null);
2020
assert.strictEqual(stdout, 'ok\n');
2121
}));
22+
23+
const entryInvalid = fixtures.path('/es-modules/cjs-exports-invalid.mjs');
24+
child = spawn(process.execPath, [entryInvalid]);
25+
let stderr = '';
26+
child.stderr.setEncoding('utf8');
27+
child.stderr.on('data', (data) => {
28+
stderr += data;
29+
});
30+
child.on('close', common.mustCall((code, signal) => {
31+
assert.strictEqual(code, 1);
32+
assert.strictEqual(signal, null);
33+
assert.ok(stderr.includes('Warning: To load an ES module'));
34+
assert.ok(stderr.includes('Unexpected token \'export\''));
35+
}));
Collapse file
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import cjs from './invalid-cjs.js';
Collapse file
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export var name = 5;

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.