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 <[email protected]>
Reviewed-By: Guy Bedford <[email protected]>
Reviewed-By: Ruben Bridgewater <[email protected]>
  • Loading branch information
MylesBorins committed Aug 5, 2020
1 parent 96fd681 commit 861a54c
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 @@ -576,6 +577,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 861a54c

Please sign in to comment.