From 45cf294495d10266f206997949f76a116f456763 Mon Sep 17 00:00:00 2001 From: Ray Date: Thu, 16 Jun 2022 18:26:36 +0800 Subject: [PATCH] module: enable subpath imports in REPL fix: https://github.com/nodejs/node/issues/43410 --- lib/internal/modules/cjs/loader.js | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) 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; + } } }