From df02a83d79d10f2a22cb83e6a452c916ad0b7b7d Mon Sep 17 00:00:00 2001 From: waqar bin zafar Date: Mon, 25 May 2026 09:46:28 +0500 Subject: [PATCH 1/2] 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'); From 78fa8095adf52fce009ae080d58848a8d3912e53 Mon Sep 17 00:00:00 2001 From: waqar bin zafar Date: Mon, 1 Jun 2026 12:31:19 +0500 Subject: [PATCH 2/2] test: avoid spawning repl import crash regression test Signed-off-by: waqar bin zafar --- .../parallel/test-repl-import-incomplete-crash.js | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/test/parallel/test-repl-import-incomplete-crash.js b/test/parallel/test-repl-import-incomplete-crash.js index bc1d48821f516c..0959a0cef5c34d 100644 --- a/test/parallel/test-repl-import-incomplete-crash.js +++ b/test/parallel/test-repl-import-incomplete-crash.js @@ -1,13 +1,10 @@ 'use strict'; -const common = require('../common'); -const child_process = require('child_process'); -const assert = require('assert'); +require('../common'); +const repl = require('repl'); // 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'); +const replserver = new repl.REPLServer(); + +replserver.emit('line', 'import'); +replserver.emit('line', '.exit');