Skip to content

Commit adee9bd

Browse files
committed
esm: import.meta.resolve exact module not found errors should return
1 parent d150316 commit adee9bd

File tree

3 files changed

+24
-9
lines changed

3 files changed

+24
-9
lines changed

lib/internal/errors.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,11 @@ function lazyUtilColors() {
195195
utilColors ??= require('internal/util/colors');
196196
return utilColors;
197197
}
198+
let url;
199+
function lazyUrl() {
200+
url ??= require('internal/url');
201+
return url;
202+
}
198203

199204
let buffer;
200205
function lazyBuffer() {
@@ -1450,7 +1455,11 @@ E('ERR_MISSING_ARGS',
14501455
return `${msg} must be specified`;
14511456
}, TypeError);
14521457
E('ERR_MISSING_OPTION', '%s is required', TypeError);
1453-
E('ERR_MODULE_NOT_FOUND', (path, base, type = 'package') => {
1458+
E('ERR_MODULE_NOT_FOUND', function(path, base, type = 'package') {
1459+
if (type === 'module') {
1460+
const { pathToFileURL } = lazyUrl();
1461+
this.url = pathToFileURL(path).href;
1462+
}
14541463
return `Cannot find ${type} '${path}' imported from ${base}`;
14551464
}, Error);
14561465
E('ERR_MULTIPLE_CALLBACK', 'Callback called multiple times', Error);

lib/internal/modules/esm/initialize_import_meta.js

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,20 @@ const experimentalImportMetaResolve = getOptionValue('--experimental-import-meta
1212
function createImportMetaResolve(defaultParentURL, loader) {
1313
return function resolve(specifier, parentURL = defaultParentURL) {
1414
let url;
15-
1615
try {
1716
({ url } = loader.resolveSync(specifier, parentURL));
17+
return url;
1818
} catch (error) {
19-
if (error?.code === 'ERR_UNSUPPORTED_DIR_IMPORT') {
20-
({ url } = error);
21-
} else {
22-
throw error;
19+
switch (error?.code) {
20+
case 'ERR_UNSUPPORTED_DIR_IMPORT':
21+
case 'ERR_MODULE_NOT_FOUND':
22+
({ url } = error);
23+
if (url) {
24+
return url;
25+
}
2326
}
27+
throw error;
2428
}
25-
26-
return url;
2729
};
2830
}
2931

test/es-module/test-esm-import-meta-resolve.mjs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,12 @@ const fixtures = dirname.slice(0, dirname.lastIndexOf('/', dirname.length - 2) +
99

1010
assert.strictEqual(import.meta.resolve('./test-esm-import-meta.mjs'),
1111
dirname + 'test-esm-import-meta.mjs');
12+
const notFound = import.meta.resolve('./notfound.mjs');
13+
assert.strictEqual(new URL(notFound).href, new URL('./notfound.mjs', import.meta.url).href);
14+
const noExtension = import.meta.resolve('./asset');
15+
assert.strictEqual(new URL(noExtension).href, new URL('./asset', import.meta.url).href);
1216
try {
13-
import.meta.resolve('./notfound.mjs');
17+
import.meta.resolve('does-not-exist');
1418
assert.fail();
1519
} catch (e) {
1620
assert.strictEqual(e.code, 'ERR_MODULE_NOT_FOUND');

0 commit comments

Comments
 (0)