Skip to content

Commit 18f01dd

Browse files
committedNov 16, 2020
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 <jasnell@gmail.com> Reviewed-By: Guy Bedford <guybedford@gmail.com> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
1 parent 77555d8 commit 18f01dd

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;
@@ -549,6 +550,14 @@ function REPLServer(prompt,
549550
e.stack = e.stack
550551
.replace(/^REPL\d+:\d+\r?\n/, '')
551552
.replace(/^\s+at\s.*\n?/gm, '');
553+
const importErrorStr = 'Cannot use import statement outside a ' +
554+
'module';
555+
if (StringPrototypeIncludes(e.message, importErrorStr)) {
556+
e.message = 'Cannot use import statement inside the Node.js ' +
557+
'repl, alternatively use dynamic import';
558+
e.stack = e.stack.replace(/SyntaxError:.*\n/,
559+
`SyntaxError: ${e.message}\n`);
560+
}
552561
} else if (self.replMode === exports.REPL_MODE_STRICT) {
553562
e.stack = e.stack.replace(/(\s+at\s+REPL\d+:)(\d+)/,
554563
(_, 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)
Please sign in to comment.