diff --git a/lib/internal/modules/package_json_reader.js b/lib/internal/modules/package_json_reader.js index d55ccbad64d0e6..92f46ed3971341 100644 --- a/lib/internal/modules/package_json_reader.js +++ b/lib/internal/modules/package_json_reader.js @@ -7,7 +7,6 @@ const { RegExpPrototypeExec, StringPrototypeIndexOf, StringPrototypeSlice, - StringPrototypeStartsWith, } = primordials; const { fileURLToPath, @@ -285,22 +284,14 @@ function findPackageJSON(specifier, base = 'data:') { validateString(specifier, 'specifier'); } - let parentURL; - if (isURL(base)) { - parentURL = new URL(base); - } else { + let parentURL = base; + if (!isURL(base)) { validateString(base, 'base'); - if ( - path.isAbsolute(base) || - (URLCanParse(base) && !StringPrototypeStartsWith(base, 'file:')) - ) { - parentURL = pathToFileURL(path.toNamespacedPath(base)); - } else { - parentURL = URL.parse(base) || pathToFileURL(base); - } + parentURL = path.isAbsolute(base) ? pathToFileURL(base) : new URL(base); } if (specifier && specifier[0] !== '.' && specifier[0] !== '/' && !URLCanParse(specifier)) { + // If `specifier` is a bare specifier. const { packageJSONPath } = getPackageJSONURL(specifier, parentURL); return packageJSONPath; }