Skip to content

Commit

Permalink
repl: improve static import error message in repl
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
MylesBorins authored and codebytere committed Aug 6, 2020
1 parent f0a41b2 commit 465c262
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
9 changes: 9 additions & 0 deletions lib/repl.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ const {
PromiseRace,
RegExp,
Set,
StringPrototypeIncludes,
Symbol,
WeakSet,
} = primordials;
Expand Down Expand Up @@ -586,6 +587,14 @@ function REPLServer(prompt,
e.stack = e.stack
.replace(/^REPL\d+:\d+\r?\n/, '')
.replace(/^\s+at\s.*\n?/gm, '');
const importErrorStr = 'Cannot use import statement outside a ' +
'module';
if (StringPrototypeIncludes(e.message, importErrorStr)) {
e.message = 'Cannot use import statement inside the Node.js ' +
'repl, alternatively use dynamic import';
e.stack = e.stack.replace(/SyntaxError:.*\n/,
`SyntaxError: ${e.message}\n`);
}
} else if (self.replMode === module.exports.REPL_MODE_STRICT) {
e.stack = e.stack.replace(/(\s+at\s+REPL\d+:)(\d+)/,
(_, pre, line) => pre + (line - 1));
Expand Down
10 changes: 10 additions & 0 deletions test/parallel/test-repl.js
Original file line number Diff line number Diff line change
Expand Up @@ -805,6 +805,16 @@ const tcpTests = [
{
send: `require(${JSON.stringify(moduleFilename)}).number`,
expect: '42'
},
{
send: 'import comeOn from \'fhqwhgads\'',
expect: [
kSource,
kArrow,
'',
'Uncaught:',
/^SyntaxError: .* dynamic import/
]
}
];

Expand Down

0 comments on commit 465c262

Please sign in to comment.