diff --git a/lib/internal/modules/esm/fetch_module.js b/lib/internal/modules/esm/fetch_module.js index 20ab9c4774d760..6cabd893ad6885 100644 --- a/lib/internal/modules/esm/fetch_module.js +++ b/lib/internal/modules/esm/fetch_module.js @@ -1,6 +1,6 @@ 'use strict'; const { - ObjectHasOwnProperty, + ObjectPrototypeHasOwnProperty, PromisePrototypeThen, SafeMap, StringPrototypeEndsWith, @@ -30,7 +30,7 @@ const { compose } = require('stream'); /** * Only for GET requests, other requests would need new Map * HTTP cache semantics keep diff caches - * + * * It caches either the promise or the cahce entry since import.meta.url needs * the value synchronously for the response location after all redirects. * @@ -104,12 +104,12 @@ function fetchWithRedirects(parsed) { const req = handler(parsed, { headers: { Accept: '*/*' }, }); - // Note that `once` is used here to handle `error` and that it hits the + // Note that `once` is used here to handle `error` and that it hits the // finally on network error/timeout. const { 0: res } = await once(req, 'response'); try { const isRedirect = res.statusCode >= 300 && res.statusCode <= 303; - const hasLocation = ObjectHasOwnProperty(res.headers, 'location'); + const hasLocation = ObjectPrototypeHasOwnProperty(res.headers, 'location'); if (isRedirect && hasLocation) { const location = new URL(res.headers.location, parsed); if (location.protocol !== 'http:' && location.protocol !== 'https:') { @@ -124,7 +124,7 @@ function fetchWithRedirects(parsed) { return entry; } if (res.statusCode === 404) { - const err = new ERR_MODULE_NOT_FOUND(parsed.href); + const err = new ERR_MODULE_NOT_FOUND(parsed.href, null); err.message = `Cannot find module '${parsed.href}', HTTP 404`; throw err; } diff --git a/test/es-module/test-http-imports.mjs b/test/es-module/test-http-imports.mjs index dfb05f3cdd12fd..f4352354e0f381 100644 --- a/test/es-module/test-http-imports.mjs +++ b/test/es-module/test-http-imports.mjs @@ -64,6 +64,11 @@ for (const { protocol, createServer } of [ const server = createServer(function(_req, res) { const url = new URL(_req.url, host); const redirect = url.searchParams.get('redirect'); + if (url.pathname === '/not-found') { + res.writeHead(404); + res.end(); + return; + } if (redirect) { const { status, location } = JSON.parse(redirect); res.writeHead(status, { @@ -164,6 +169,12 @@ for (const { protocol, createServer } of [ import(unsupportedMIME.href), 'should not be able to load unsupported MIMEs from http:' ); + const notFound = new URL(url.href); + notFound.pathname = '/not-found'; + await assert.rejects( + import(notFound.href), + { code: 'ERR_MODULE_NOT_FOUND' }, + ); server.close(); }