Skip to content

Commit a084632

Browse files
lundibundiTrott
authored andcommitted
esm: improve error message of ERR_UNSUPPORTED_ESM_URL_SCHEME
Refs: #34765 PR-URL: #34795 Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]> Reviewed-By: Jan Krems <[email protected]> Reviewed-By: Guy Bedford <[email protected]> Reviewed-By: Bradley Farias <[email protected]> Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Rich Trott <[email protected]>
1 parent f39a121 commit a084632

File tree

3 files changed

+26
-12
lines changed

3 files changed

+26
-12
lines changed

lib/internal/errors.js

+11-2
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ const {
2626
WeakMap,
2727
} = primordials;
2828

29+
const isWindows = process.platform === 'win32';
30+
2931
const messages = new Map();
3032
const codes = {};
3133

@@ -1429,8 +1431,15 @@ E('ERR_UNKNOWN_MODULE_FORMAT', 'Unknown module format: %s', RangeError);
14291431
E('ERR_UNKNOWN_SIGNAL', 'Unknown signal: %s', TypeError);
14301432
E('ERR_UNSUPPORTED_DIR_IMPORT', "Directory import '%s' is not supported " +
14311433
'resolving ES modules imported from %s', Error);
1432-
E('ERR_UNSUPPORTED_ESM_URL_SCHEME', 'Only file and data URLs are supported ' +
1433-
'by the default ESM loader', Error);
1434+
E('ERR_UNSUPPORTED_ESM_URL_SCHEME', (url) => {
1435+
let msg = 'Only file and data URLs are supported by the default ESM loader';
1436+
if (isWindows && url.protocol.length === 2) {
1437+
msg += '. Absolute Windows paths without prefix are not valid URLs, ' +
1438+
"consider using 'file://' prefix";
1439+
}
1440+
msg += `. Received protocol '${url.protocol}'`;
1441+
return msg;
1442+
}, Error);
14341443

14351444
// This should probably be a `TypeError`.
14361445
E('ERR_VALID_PERFORMANCE_ENTRY_TYPE',

lib/internal/modules/esm/resolve.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -747,7 +747,7 @@ function defaultResolve(specifier, context = {}, defaultResolveUnused) {
747747
if (parsed && parsed.protocol === 'nodejs:')
748748
return { url: specifier };
749749
if (parsed && parsed.protocol !== 'file:' && parsed.protocol !== 'data:')
750-
throw new ERR_UNSUPPORTED_ESM_URL_SCHEME();
750+
throw new ERR_UNSUPPORTED_ESM_URL_SCHEME(parsed);
751751
if (NativeModule.canBeRequiredByUsers(specifier)) {
752752
return {
753753
url: 'nodejs:' + specifier

test/es-module/test-esm-dynamic-import.js

+14-9
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,11 @@ const absolutePath = require.resolve('../fixtures/es-modules/test-esm-ok.mjs');
88
const targetURL = new URL('file:///');
99
targetURL.pathname = absolutePath;
1010

11-
function expectErrorProperty(result, propertyKey, value) {
12-
Promise.resolve(result)
13-
.catch(common.mustCall((error) => {
14-
assert.strictEqual(error[propertyKey], value);
15-
}));
16-
}
17-
18-
function expectModuleError(result, err) {
19-
expectErrorProperty(result, 'code', err);
11+
function expectModuleError(result, code, message) {
12+
Promise.resolve(result).catch(common.mustCall((error) => {
13+
assert.strictEqual(error.code, code);
14+
if (message) assert.strictEqual(error.message, message);
15+
}));
2016
}
2117

2218
function expectOkNamespace(result) {
@@ -60,4 +56,13 @@ function expectFsNamespace(result) {
6056
'ERR_MODULE_NOT_FOUND');
6157
expectModuleError(import('http://example.com/foo.js'),
6258
'ERR_UNSUPPORTED_ESM_URL_SCHEME');
59+
if (common.isWindows) {
60+
const msg =
61+
'Only file and data URLs are supported by the default ESM loader. ' +
62+
'Absolute Windows paths without prefix are not valid URLs, ' +
63+
"consider using 'file://' prefix. Received protocol 'c:'";
64+
expectModuleError(import('C:\\example\\foo.mjs'),
65+
'ERR_UNSUPPORTED_ESM_URL_SCHEME',
66+
msg);
67+
}
6368
})();

0 commit comments

Comments
 (0)