diff --git a/graal-nodejs/lib/internal/errors.js b/graal-nodejs/lib/internal/errors.js index 190afad2845..0e649126aca 100644 --- a/graal-nodejs/lib/internal/errors.js +++ b/graal-nodejs/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/graal-nodejs/lib/internal/modules/esm/fetch_module.js b/graal-nodejs/lib/internal/modules/esm/fetch_module.js index 74d2d2599db..ca5c9c83c31 100644 --- a/graal-nodejs/lib/internal/modules/esm/fetch_module.js +++ b/graal-nodejs/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/graal-nodejs/lib/internal/modules/esm/initialize_import_meta.js b/graal-nodejs/lib/internal/modules/esm/initialize_import_meta.js index 010f4d1a858..c19050ade01 100644 --- a/graal-nodejs/lib/internal/modules/esm/initialize_import_meta.js +++ b/graal-nodejs/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/graal-nodejs/lib/internal/modules/esm/resolve.js b/graal-nodejs/lib/internal/modules/esm/resolve.js index fba9667ea1a..2f7e4fca52b 100644 --- a/graal-nodejs/lib/internal/modules/esm/resolve.js +++ b/graal-nodejs/lib/internal/modules/esm/resolve.js @@ -289,7 +289,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) { @@ -845,7 +845,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/graal-nodejs/test/es-module/test-esm-import-meta-resolve.mjs b/graal-nodejs/test/es-module/test-esm-import-meta-resolve.mjs index 22139122eb5..5886af019b0 100644 --- a/graal-nodejs/test/es-module/test-esm-import-meta-resolve.mjs +++ b/graal-nodejs/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');