8000
We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 6257408 commit b91d22cCopy full SHA for b91d22c
lib/repl.js
@@ -332,10 +332,12 @@ function REPLServer(prompt,
332
if (code === '\n')
333
return cb(null);
334
335
- let pwd;
+ let parentURL;
336
try {
337
const { pathToFileURL } = require('url');
338
- pwd = pathToFileURL(process.cwd()).href;
+ // Adding `/repl` prevents dynamic imports from loading relative
339
+ // to the parent of `process.cwd()`.
340
+ parentURL = pathToFileURL(path.join(process.cwd(), 'repl')).href;
341
} catch {
342
}
343
while (true) {
@@ -350,7 +352,7 @@ function REPLServer(prompt,
350
352
filename: file,
351
353
displayErrors: true,
354
importModuleDynamically: async (specifier) => {
- return asyncESM.ESMLoader.import(specifier, pwd);
355
+ return asyncESM.ESMLoader.import(specifier, parentURL);
356
357
});
358
} catch (e) {
test/parallel/test-repl-import-referrer.js
@@ -0,0 +1,24 @@
1
+'use strict';
2
+const common = require('../common');
3
+const assert = require('assert');
4
+const cp = require('child_process');
5
+const fixtures = require('../common/fixtures');
6
+
7
+const args = ['--interactive', '--experimental-repl-await'];
8
+const opts = { cwd: fixtures.path('es-modules') };
9
+const child = cp.spawn(process.execPath, args, opts);
10
11
+let output = '';
12
+child.stdout.setEncoding('utf8');
13
+child.stdout.on('data', (data) => {
14
+ output += data;
15
+});
16
17
+child.on('exit', common.mustCall(() => {
18
+ const results = output.replace(/^> /mg, '').split('\n').slice(2);
19
+ assert.deepStrictEqual(results, ['[Module] { message: \'A message\' }', '']);
20
+}));
21
22
+child.stdin.write('await import(\'./message.mjs\');\n');
23
+child.stdin.write('.exit');
24
+child.stdin.end();