diff --git a/lib/internal/modules/cjs/loader.js b/lib/internal/modules/cjs/loader.js index 34327a70045fca..085674530f59a5 100644 --- a/lib/internal/modules/cjs/loader.js +++ b/lib/internal/modules/cjs/loader.js @@ -900,8 +900,8 @@ Module._resolveFilename = function(request, parent, isMain, options) { paths = Module._resolveLookupPaths(request, parent); } - if (parent?.filename) { - if (request[0] === '#') { + if (request[0] === '#') { + if (parent?.filename) { const pkg = readPackageScope(parent.filename) || {}; if (pkg.data?.imports != null) { try { @@ -915,6 +915,20 @@ Module._resolveFilename = function(request, parent, isMain, options) { throw e; } } + } else if (parent.id === '') { + // Also enable package imports in REPL + const curloc = process.cwd() + path.sep; + try { + return finalizeEsmResolution( + packageImportsResolve( + request, pathToFileURL(curloc), + cjsConditions), + curloc, curloc); + } catch (e) { + if (e.code === 'ERR_MODULE_NOT_FOUND') + throw createEsmNotFoundErr(request); + throw e; + } } }