Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions lib/internal/main/repl.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,9 @@ cliRepl.createInternalRepl(process.env, (err, repl) => {

// If user passed '-e' or '--eval' along with `-i` or `--interactive`,
// evaluate the code in the current context.
const source = getOptionValue('--eval');
if (source != null) {
if (getOptionValue('[has_eval_string]')) {
evalScript('[eval]',
source,
getOptionValue('--eval'),
getOptionValue('--inspect-brk'),
getOptionValue('--print'));
}
4 changes: 4 additions & 0 deletions lib/internal/process/execution.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ function evalScript(name, body, breakFirstLine, printResult) {
const { kVmBreakFirstLineSymbol } = require('internal/util');

const cwd = tryGetCwd();
const origModule = global.module; // Set e.g. when called from the REPL.

const module = new CJSModule(name);
module.filename = path.join(cwd, name);
Expand All @@ -79,6 +80,9 @@ function evalScript(name, body, breakFirstLine, printResult) {
const { kStdout, print } = require('internal/util/print');
print(kStdout, result);
}

if (origModule !== undefined)
global.module = origModule;
}

const exceptionHandlerState = { captureFn: null };
Expand Down
22 changes: 22 additions & 0 deletions test/parallel/test-repl-cli-eval.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
'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/27575:
// module.id === '<repl>' in the REPL.

for (const extraFlags of [[], ['-e', '42']]) {
const flags = ['--interactive', ...extraFlags];
const proc = child_process.spawn(process.execPath, flags, {
stdio: ['pipe', 'pipe', 'inherit']
});
proc.stdin.write('module.id\n.exit\n');

let stdout = '';
proc.stdout.setEncoding('utf8');
proc.stdout.on('data', (chunk) => stdout += chunk);
proc.stdout.on('end', common.mustCall(() => {
assert(stdout.includes('<repl>'), `stdout: ${stdout}`);
}));
}