diff --git a/lib/internal/modules/esm/resolve.js b/lib/internal/modules/esm/resolve.js index 987a139c6aae57..f93dd44e1f2147 100644 --- a/lib/internal/modules/esm/resolve.js +++ b/lib/internal/modules/esm/resolve.js @@ -133,8 +133,9 @@ function getPackageScopeConfig(resolved, base) { const packageJSONPath = packageJSONUrl.pathname; if (StringPrototypeEndsWith(packageJSONPath, 'node_modules/package.json')) break; - const packageConfig = getPackageConfig(fileURLToPath(packageJSONUrl), base); - if (packageConfig.exists) return packageConfig; + const filePath = fileURLToPath(packageJSONUrl); + const packageConfig = getPackageConfig(filePath, base); + if (packageConfig.exists) return { path: filePath, packageConfig }; const lastPackageJSONUrl = packageJSONUrl; packageJSONUrl = new URL('../package.json', packageJSONUrl); @@ -151,7 +152,7 @@ function getPackageScopeConfig(resolved, base) { exports: undefined }; packageJSONCache.set(fileURLToPath(packageJSONUrl), packageConfig); - return packageConfig; + return { packageConfig }; } /* @@ -488,7 +489,7 @@ function packageExportsResolve( } function getPackageType(url) { - const packageConfig = getPackageScopeConfig(url, url); + const { packageConfig } = getPackageScopeConfig(url, url); return packageConfig.type; } @@ -533,17 +534,10 @@ function packageResolve(specifier, base, conditions) { '' : '.' + StringPrototypeSlice(specifier, separatorIndex); // ResolveSelf - const packageConfig = getPackageScopeConfig(base, base); + const { path, packageConfig } = getPackageScopeConfig(base, base); if (packageConfig.exists) { - // TODO(jkrems): Find a way to forward the pair/iterator already generated - // while executing GetPackageScopeConfig - let packageJSONUrl; - for (const [ filename, packageConfigCandidate ] of packageJSONCache) { - if (packageConfig === packageConfigCandidate) { - packageJSONUrl = pathToFileURL(filename); - break; - } - } + // If path not exist, then packageConfig.exists is false too + const packageJSONUrl = pathToFileURL(path); if (packageJSONUrl !== undefined && packageConfig.name === packageName && packageConfig.exports !== undefined) {