From df02a83d79d10f2a22cb83e6a452c916ad0b7b7d Mon Sep 17 00:00:00 2001 From: waqar bin zafar Date: Mon, 25 May 2026 09:46:28 +0500 Subject: [PATCH] repl: fix crash when bare 'import' is typed --- lib/repl.js | 7 ++++++- test/parallel/test-repl-import-incomplete-crash.js | 13 +++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) create mode 100644 test/parallel/test-repl-import-incomplete-crash.js diff --git a/lib/repl.js b/lib/repl.js index 60ea4457ab1bf6..5a5b4acdd5e860 100644 --- a/lib/repl.js +++ b/lib/repl.js @@ -250,7 +250,12 @@ writer.options = { ...inspect.defaultOptions, showProxy: true }; // Converts static import statement to dynamic import statement const toDynamicImport = (codeLine) => { let dynamicImportStatement = ''; - const ast = acornParse(codeLine, { __proto__: null, sourceType: 'module', ecmaVersion: 'latest' }); + let ast; + try { + ast = acornParse(codeLine, { __proto__: null, sourceType: 'module', ecmaVersion: 'latest' }); + } catch { + return codeLine; + } acornWalk.ancestor(ast, { ImportDeclaration(node) { const awaitDynamicImport = `await import(${JSONStringify(node.source.value)});`; diff --git a/test/parallel/test-repl-import-incomplete-crash.js b/test/parallel/test-repl-import-incomplete-crash.js new file mode 100644 index 00000000000000..bc1d48821f516c --- /dev/null +++ b/test/parallel/test-repl-import-incomplete-crash.js @@ -0,0 +1,13 @@ +'use strict'; +const common = require('../common'); +const child_process = require('child_process'); +const assert = require('assert'); + +// Regression test for https://github.com/nodejs/node/issues/63551: +// Typing a bare `import` keyword in the REPL must not crash the process. +const proc = child_process.spawn(process.execPath, ['-i']); +proc.on('error', common.mustNotCall()); +proc.on('exit', common.mustCall((code) => { + assert.strictEqual(code, 0); +})); +proc.stdin.write('import\n.exit\n');