From 4c6e28cb14c7e70b9d582d98601e59e29e79d270 Mon Sep 17 00:00:00 2001 From: Ray Date: Fri, 17 Jun 2022 13:52:42 +0800 Subject: [PATCH] module: also enable subpath imports in REPL --- lib/internal/modules/cjs/loader.js | 27 +++++++++++++-------------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/lib/internal/modules/cjs/loader.js b/lib/internal/modules/cjs/loader.js index 34327a70045fca5..055f6150bb54676 100644 --- a/lib/internal/modules/cjs/loader.js +++ b/lib/internal/modules/cjs/loader.js @@ -900,20 +900,19 @@ Module._resolveFilename = function(request, parent, isMain, options) { paths = Module._resolveLookupPaths(request, parent); } - if (parent?.filename) { - if (request[0] === '#') { - const pkg = readPackageScope(parent.filename) || {}; - if (pkg.data?.imports != null) { - try { - return finalizeEsmResolution( - packageImportsResolve(request, pathToFileURL(parent.filename), - cjsConditions), parent.filename, - pkg.path); - } catch (e) { - if (e.code === 'ERR_MODULE_NOT_FOUND') - throw createEsmNotFoundErr(request); - throw e; - } + if (request[0] === '#' && (parent?.filename || parent?.id === '')) { + const parentPath = parent?.filename ?? process.cwd() + path.sep; + const pkg = readPackageScope(parentPath) || {}; + if (pkg.data?.imports != null) { + try { + return finalizeEsmResolution( + packageImportsResolve(request, pathToFileURL(parentPath), + cjsConditions), parentPath, + pkg.path); + } catch (e) { + if (e.code === 'ERR_MODULE_NOT_FOUND') + throw createEsmNotFoundErr(request); + throw e; } } }