diff --git a/lib/internal/errors.js b/lib/internal/errors.js index a335cca7df11db..bed02da76a3bab 100644 --- a/lib/internal/errors.js +++ b/lib/internal/errors.js @@ -1453,8 +1453,12 @@ E('ERR_MISSING_ARGS', return `${msg} must be specified`; }, TypeError); E('ERR_MISSING_OPTION', '%s is required', TypeError); -E('ERR_MODULE_NOT_FOUND', (path, base, type = 'package') => { - return `Cannot find ${type} '${path}' imported from ${base}`; +E('ERR_MODULE_NOT_FOUND', function(path, base, exactUrl) { + if (exactUrl) { + lazyInternalUtil().setOwnProperty(this, 'url', exactUrl); + } + return `Cannot find ${ + exactUrl ? 'module' : 'package'} '${path}' imported from ${base}`; }, Error); E('ERR_MULTIPLE_CALLBACK', 'Callback called multiple times', Error); E('ERR_NAPI_CONS_FUNCTION', 'Constructor must be a function', TypeError); diff --git a/lib/internal/modules/esm/fetch_module.js b/lib/internal/modules/esm/fetch_module.js index 74d2d2599dbd45..ca5c9c83c316de 100644 --- a/lib/internal/modules/esm/fetch_module.js +++ b/lib/internal/modules/esm/fetch_module.js @@ -144,7 +144,7 @@ function fetchWithRedirects(parsed) { return entry; } if (res.statusCode === 404) { - const err = new ERR_MODULE_NOT_FOUND(parsed.href, null); + const err = new ERR_MODULE_NOT_FOUND(parsed.href, null, parsed); err.message = `Cannot find module '${parsed.href}', HTTP 404`; throw err; } diff --git a/lib/internal/modules/esm/initialize_import_meta.js b/lib/internal/modules/esm/initialize_import_meta.js index 010f4d1a858690..c19050ade018d0 100644 --- a/lib/internal/modules/esm/initialize_import_meta.js +++ b/lib/internal/modules/esm/initialize_import_meta.js @@ -12,18 +12,20 @@ const experimentalImportMetaResolve = getOptionValue('--experimental-import-meta function createImportMetaResolve(defaultParentURL, loader) { return function resolve(specifier, parentURL = defaultParentURL) { let url; - try { ({ url } = loader.resolveSync(specifier, parentURL)); + return url; } catch (error) { - if (error?.code === 'ERR_UNSUPPORTED_DIR_IMPORT') { - ({ url } = error); - } else { - throw error; + switch (error?.code) { + case 'ERR_UNSUPPORTED_DIR_IMPORT': + case 'ERR_MODULE_NOT_FOUND': + ({ url } = error); + if (url) { + return url; + } } + throw error; } - - return url; }; } diff --git a/lib/internal/modules/esm/resolve.js b/lib/internal/modules/esm/resolve.js index 8160206da4c044..6d1793c06a4270 100644 --- a/lib/internal/modules/esm/resolve.js +++ b/lib/internal/modules/esm/resolve.js @@ -222,7 +222,7 @@ function finalizeResolution(resolved, base, preserveSymlinks) { process.send({ 'watch:require': [path || resolved.pathname] }); } throw new ERR_MODULE_NOT_FOUND( - path || resolved.pathname, base && fileURLToPath(base), 'module'); + path || resolved.pathname, base && fileURLToPath(base), resolved); } if (!preserveSymlinks) { @@ -777,7 +777,7 @@ function packageResolve(specifier, base, conditions) { // eslint can't handle the above code. // eslint-disable-next-line no-unreachable - throw new ERR_MODULE_NOT_FOUND(packageName, fileURLToPath(base)); + throw new ERR_MODULE_NOT_FOUND(packageName, fileURLToPath(base), null); } /** diff --git a/test/es-module/test-esm-import-meta-resolve.mjs b/test/es-module/test-esm-import-meta-resolve.mjs index ec6cd37ab01e10..5ae4d256886a1f 100644 --- a/test/es-module/test-esm-import-meta-resolve.mjs +++ b/test/es-module/test-esm-import-meta-resolve.mjs @@ -9,8 +9,12 @@ const fixtures = dirname.slice(0, dirname.lastIndexOf('/', dirname.length - 2) + assert.strictEqual(import.meta.resolve('./test-esm-import-meta.mjs'), dirname + 'test-esm-import-meta.mjs'); +const notFound = import.meta.resolve('./notfound.mjs'); +assert.strictEqual(new URL(notFound).href, new URL('./notfound.mjs', import.meta.url).href); +const noExtension = import.meta.resolve('./asset'); +assert.strictEqual(new URL(noExtension).href, new URL('./asset', import.meta.url).href); try { - import.meta.resolve('./notfound.mjs'); + import.meta.resolve('does-not-exist'); assert.fail(); } catch (e) { assert.strictEqual(e.code, 'ERR_MODULE_NOT_FOUND');