Skip to content

Commit 3ad4d37

Browse files
rayw000targos
authored andcommitted
module: also enable subpath imports in REPL
PR-URL: #43450 Fixes: #43410 Reviewed-By: Guy Bedford <[email protected]> Reviewed-By: Antoine du Hamel <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 12a591a commit 3ad4d37

File tree

2 files changed

+32
-14
lines changed

2 files changed

+32
-14
lines changed

lib/internal/modules/cjs/loader.js

+13-14
Original file line numberDiff line numberDiff line change
@@ -904,20 +904,19 @@ Module._resolveFilename = function(request, parent, isMain, options) {
904904
paths = Module._resolveLookupPaths(request, parent);
905905
}
906906

907-
if (parent?.filename) {
908-
if (request[0] === '#') {
909-
const pkg = readPackageScope(parent.filename) || {};
910-
if (pkg.data?.imports != null) {
911-
try {
912-
return finalizeEsmResolution(
913-
packageImportsResolve(request, pathToFileURL(parent.filename),
914-
cjsConditions), parent.filename,
915-
pkg.path);
916-
} catch (e) {
917-
if (e.code === 'ERR_MODULE_NOT_FOUND')
918-
throw createEsmNotFoundErr(request);
919-
throw e;
920-
}
907+
if (request[0] === '#' && (parent?.filename || parent?.id === '<repl>')) {
908+
const parentPath = parent?.filename ?? process.cwd() + path.sep;
909+
const pkg = readPackageScope(parentPath) || {};
910+
if (pkg.data?.imports != null) {
911+
try {
912+
return finalizeEsmResolution(
913+
packageImportsResolve(request, pathToFileURL(parentPath),
914+
cjsConditions), parentPath,
915+
pkg.path);
916+
} catch (e) {
917+
if (e.code === 'ERR_MODULE_NOT_FOUND')
918+
throw createEsmNotFoundErr(request);
919+
throw e;
921920
}
922921
}
923922
}
+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
'use strict';
2+
const { mustCall } = require('../common');
3+
const assert = require('assert');
4+
const fixtures = require('../common/fixtures');
5+
const { spawn } = require('child_process');
6+
7+
const child = spawn(process.execPath, [
8+
'--interactive',
9+
], {
10+
cwd: fixtures.path('es-modules', 'pkgimports'),
11+
});
12+
13+
child.stdin.end(
14+
'try{require("#test");await import("#test")}catch{process.exit(-1)}'
15+
);
16+
17+
child.on('exit', mustCall((code) => {
18+
assert.strictEqual(code, 0);
19+
}));

0 commit comments

Comments
 (0)