diff --git a/lib/internal/modules/cjs/helpers.js b/lib/internal/modules/cjs/helpers.js index dd15055aee1fd5..00d2b628c44172 100644 --- a/lib/internal/modules/cjs/helpers.js +++ b/lib/internal/modules/cjs/helpers.js @@ -87,19 +87,17 @@ function makeRequireFunction(mod, redirects) { if (destination === true) { missing = false; } else if (destination) { - const href = destination.href; - if (destination.protocol === 'node:') { + const { href, protocol } = destination; + if (protocol === 'node:') { const specifier = destination.pathname; const mod = loadBuiltinModule(specifier, href); if (mod && mod.canBeRequiredByUsers) { return mod.exports; } throw new ERR_UNKNOWN_BUILTIN_MODULE(specifier); - } else if (destination.protocol === 'file:') { - let filepath; - if (urlToFileCache.has(href)) { - filepath = urlToFileCache.get(href); - } else { + } else if (protocol === 'file:') { + let filepath = urlToFileCache.get(href); + if (!filepath) { filepath = fileURLToPath(destination); urlToFileCache.set(href, filepath); }