Skip to content

Commit 465c262

Browse files
MylesBorinscodebytere
authored andcommitted
repl: improve static import error message in repl
Currently the error is rather ambiguous and does not inform folks that static import is not supported in the repl. This overrides the default error message with one that is more informative. Closes: #33576 PR-URL: #33588 Fixes: #33576 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Guy Bedford <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]>
1 parent f0a41b2 commit 465c262

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

lib/repl.js

+9
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ const {
5858
PromiseRace,
5959
RegExp,
6060
Set,
61+
StringPrototypeIncludes,
6162
Symbol,
6263
WeakSet,
6364
} = primordials;
@@ -586,6 +587,14 @@ function REPLServer(prompt,
586587
e.stack = e.stack
587588
.replace(/^REPL\d+:\d+\r?\n/, '')
588589
.replace(/^\s+at\s.*\n?/gm, '');
590+
const importErrorStr = 'Cannot use import statement outside a ' +
591+
'module';
592+
if (StringPrototypeIncludes(e.message, importErrorStr)) {
593+
e.message = 'Cannot use import statement inside the Node.js ' +
594+
'repl, alternatively use dynamic import';
595+
e.stack = e.stack.replace(/SyntaxError:.*\n/,
596+
`SyntaxError: ${e.message}\n`);
597+
}
589598
} else if (self.replMode === module.exports.REPL_MODE_STRICT) {
590599
e.stack = e.stack.replace(/(\s+at\s+REPL\d+:)(\d+)/,
591600
(_, pre, line) => pre + (line - 1));

test/parallel/test-repl.js

+10
Original file line numberDiff line numberDiff line change
@@ -805,6 +805,16 @@ const tcpTests = [
805805
{
806806
send: `require(${JSON.stringify(moduleFilename)}).number`,
807807
expect: '42'
808+
},
809+
{
810+
send: 'import comeOn from \'fhqwhgads\'',
811+
expect: [
812+
kSource,
813+
kArrow,
814+
'',
815+
'Uncaught:',
816+
/^SyntaxError: .* dynamic import/
817+
]
808818
}
809819
];
810820

0 commit comments

Comments
 (0)