Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 551be2a

Browse filesBrowse files
lundibundicodebytere
authored andcommitted
esm: improve error message of ERR_UNSUPPORTED_ESM_URL_SCHEME
Refs: #34765 PR-URL: #34795 Backport-PR-URL: #35385 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: Jan Krems <jan.krems@gmail.com> Reviewed-By: Guy Bedford <guybedford@gmail.com> Reviewed-By: Bradley Farias <bradley.meck@gmail.com> Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Rich Trott <rtrott@gmail.com>
1 parent fc93cc9 commit 551be2a
Copy full SHA for 551be2a

File tree

Expand file treeCollapse file tree

3 files changed

+26
-12
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

3 files changed

+26
-12
lines changed
Open diff view settings
Collapse file

‎lib/internal/errors.js‎

Copy file name to clipboardExpand all lines: lib/internal/errors.js
+11-2Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ const {
2727
WeakMap,
2828
} = primordials;
2929

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

@@ -1399,8 +1401,15 @@ E('ERR_UNKNOWN_MODULE_FORMAT', 'Unknown module format: %s', RangeError);
13991401
E('ERR_UNKNOWN_SIGNAL', 'Unknown signal: %s', TypeError);
14001402
E('ERR_UNSUPPORTED_DIR_IMPORT', "Directory import '%s' is not supported " +
14011403
'resolving ES modules imported from %s', Error);
1402-
E('ERR_UNSUPPORTED_ESM_URL_SCHEME', 'Only file and data URLs are supported ' +
1403-
'by the default ESM loader', Error);
1404+
E('ERR_UNSUPPORTED_ESM_URL_SCHEME', (url) => {
1405+
let msg = 'Only file and data URLs are supported by the default ESM loader';
1406+
if (isWindows && url.protocol.length === 2) {
1407+
msg += '. Absolute Windows paths without prefix are not valid URLs, ' +
1408+
"consider using 'file://' prefix";
1409+
}
1410+
msg += `. Received protocol '${url.protocol}'`;
1411+
return msg;
1412+
}, Error);
14041413

14051414
// This should probably be a `TypeError`.
14061415
E('ERR_VALID_PERFORMANCE_ENTRY_TYPE',
Collapse file

‎lib/internal/modules/esm/resolve.js‎

Copy file name to clipboardExpand all lines: lib/internal/modules/esm/resolve.js
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -722,7 +722,7 @@ function defaultResolve(specifier, context = {}, defaultResolveUnused) {
722722
if (parsed && parsed.protocol === 'nodejs:')
723723
return { url: specifier };
724724
if (parsed && parsed.protocol !== 'file:' && parsed.protocol !== 'data:')
725-
throw new ERR_UNSUPPORTED_ESM_URL_SCHEME();
725+
throw new ERR_UNSUPPORTED_ESM_URL_SCHEME(parsed);
726726
if (NativeModule.canBeRequiredByUsers(specifier)) {
727727
return {
728728
url: 'nodejs:' + specifier
Collapse file

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

Copy file name to clipboardExpand all lines: test/es-module/test-esm-dynamic-import.js
+14-9Lines changed: 14 additions & 9 deletions
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) {
@@ -63,4 +59,13 @@ function expectFsNamespace(result) {
6359
'ERR_MODULE_NOT_FOUND');
6460
expectModuleError(import('http://example.com/foo.js'),
6561
'ERR_UNSUPPORTED_ESM_URL_SCHEME');
62+
if (common.isWindows) {
63+
const msg =
64+
'Only file and data URLs are supported by the default ESM loader. ' +
65+
'Absolute Windows paths without prefix are not valid URLs, ' +
66+
"consider using 'file://' prefix. Received protocol 'c:'";
67+
expectModuleError(import('C:\\example\\foo.mjs'),
68+
'ERR_UNSUPPORTED_ESM_URL_SCHEME',
69+
msg);
70+
}
6671
})();

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.