Skip to content

repl: fix current path for dynamic module imports #30060

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Closed
wants to merge 2 commits into from
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
2 changes: 1 addition & 1 deletion lib/repl.js
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ function REPLServer(prompt,
let pwd;
try {
const { pathToFileURL } = require('url');
pwd = pathToFileURL(process.cwd()).href;
pwd = pathToFileURL(`${process.cwd()}/`).href;
} catch {
}
while (true) {
Expand Down
46 changes: 46 additions & 0 deletions test/parallel/test-repl-dynamic-import.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
'use strict';

// Flags: --experimental-modules

const common = require('../common');
const fixtures = require('../common/fixtures');
const assert = require('assert');
const net = require('net');

if (!common.isMainThread)
common.skip('process.chdir is not available in Workers');

process.chdir(fixtures.fixturesDir);
const repl = require('repl');

const server = net.createServer((conn) => {
repl.start('', conn).on('exit', () => {
conn.destroy();
server.close();
});
});

const host = common.localhostIPv4;
const port = 0;
const options = { host, port };

let answer = '';
let expectedDataEvents = 2;
server.listen(options, function() {
options.port = this.address().port;
const conn = net.connect(options);
conn.setEncoding('utf8');
conn.on('data', (data) => {
answer += data;
if (--expectedDataEvents === 0) {
conn.write('.exit\n');
}
});
conn.write('import("./es-modules/message.mjs").then(console.log)\n');
});

process.on('exit', function() {
assert.strictEqual(/Cannot find module/.test(answer), false);
assert.strictEqual(/Error/.test(answer), false);
assert.strictEqual(/{ message: 'A message' }/.test(answer), true);
});