Skip to content

Commit

Permalink
esm: import.meta.resolve exact module not found errors should return
Browse files Browse the repository at this point in the history
PR-URL: nodejs#49038
Reviewed-By: Geoffrey Booth <[email protected]>
Reviewed-By: Jan Krems <[email protected]>
Reviewed-By: Antoine du Hamel <[email protected]>
Reviewed-By: Mohammed Keyvanzadeh <[email protected]>
Reviewed-By: Jacob Smith <[email protected]>
  • Loading branch information
guybedford authored and targos committed Nov 10, 2023
1 parent 0bfcddc commit a256f11
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 13 deletions.
8 changes: 6 additions & 2 deletions lib/internal/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion lib/internal/modules/esm/fetch_module.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
16 changes: 9 additions & 7 deletions lib/internal/modules/esm/initialize_import_meta.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
}

Expand Down
4 changes: 2 additions & 2 deletions lib/internal/modules/esm/resolve.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
}

/**
Expand Down
6 changes: 5 additions & 1 deletion test/es-module/test-esm-import-meta-resolve.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down

0 comments on commit a256f11

Please sign in to comment.