Skip to content

Commit 3f52f6a

Browse files
committed
module: enable subpath imports in REPL
fix: #43410
1 parent 6975dd1 commit 3f52f6a

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

lib/internal/modules/cjs/loader.js

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -900,8 +900,8 @@ Module._resolveFilename = function(request, parent, isMain, options) {
900900
paths = Module._resolveLookupPaths(request, parent);
901901
}
902902

903-
if (parent?.filename) {
904-
if (request[0] === '#') {
903+
if (request[0] === '#') {
904+
if (parent?.filename) {
905905
const pkg = readPackageScope(parent.filename) || {};
906906
if (pkg.data?.imports != null) {
907907
try {
@@ -915,6 +915,20 @@ Module._resolveFilename = function(request, parent, isMain, options) {
915915
throw e;
916916
}
917917
}
918+
} else if (parent?.id === '<repl>') {
919+
// Also enable package imports in REPL
920+
const curloc = process.cwd() + path.sep;
921+
try {
922+
return finalizeEsmResolution(
923+
packageImportsResolve(
924+
request, pathToFileURL(curloc),
925+
cjsConditions),
926+
curloc, curloc);
927+
} catch (e) {
928+
if (e.code === 'ERR_MODULE_NOT_FOUND')
929+
throw createEsmNotFoundErr(request);
930+
throw e;
931+
}
918932
}
919933
}
920934

0 commit comments

Comments
 (0)