Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
MylesBorins committed Nov 16, 2020
1 parent 77555d8 commit 18f01dd
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
Expand Up @@ -58,6 +58,7 @@ const {
PromiseRace,
RegExp,
Set,
StringPrototypeIncludes,
Symbol,
WeakSet,
} = primordials;
Expand Down Expand Up @@ -549,6 +550,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 === 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
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 18f01dd

Please sign in to comment.