From 4a7fe9a73caf118caa2b07c526929be1bf8027ca Mon Sep 17 00:00:00 2001 From: Myles Borins Date: Wed, 27 May 2020 12:05:55 -0400 Subject: [PATCH] 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: https://github.com/nodejs/node/issues/33576 --- lib/repl.js | 9 +++++++++ test/parallel/test-repl.js | 10 ++++++++++ 2 files changed, 19 insertions(+) diff --git a/lib/repl.js b/lib/repl.js index cc5bf069c31295..eea6db94a3558c 100644 --- a/lib/repl.js +++ b/lib/repl.js @@ -58,6 +58,7 @@ const { PromiseRace, RegExp, Set, + StringPrototypeIncludes, Symbol, WeakSet, } = primordials; @@ -570,6 +571,14 @@ function REPLServer(prompt, e.stack = e.stack .replace(/^repl:\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+)/, (_, pre, line) => pre + (line - 1)); diff --git a/test/parallel/test-repl.js b/test/parallel/test-repl.js index 71160a20ec86ab..8f98dfd540ace8 100644 --- a/test/parallel/test-repl.js +++ b/test/parallel/test-repl.js @@ -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/ + ] } ];