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 8f38c19

Browse filesBrowse files
lundibundiBethGriggs
authored andcommitted
esm: improve error message of ERR_UNSUPPORTED_ESM_URL_SCHEME
Refs: #34765 PR-URL: #34795 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 969fb1c commit 8f38c19
Copy full SHA for 8f38c19

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
@@ -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

@@ -1410,8 +1412,15 @@ E('ERR_UNKNOWN_MODULE_FORMAT', 'Unknown module format: %s', RangeError);
14101412
E('ERR_UNKNOWN_SIGNAL', 'Unknown signal: %s', TypeError);
14111413
E('ERR_UNSUPPORTED_DIR_IMPORT', "Directory import '%s' is not supported " +
14121414
'resolving ES modules imported from %s', Error);
1413-
E('ERR_UNSUPPORTED_ESM_URL_SCHEME', 'Only file and data URLs are supported ' +
1414-
'by the default ESM loader', Error);
1415+
E('ERR_UNSUPPORTED_ESM_URL_SCHEME', (url) => {
1416+
let msg = 'Only file and data URLs are supported by the default ESM loader';
1417+
if (isWindows && url.protocol.length === 2) {
1418+
msg += '. Absolute Windows paths without prefix are not valid URLs, ' +
1419+
"consider using 'file://' prefix";
1420+
}
1421+
msg += `. Received protocol '${url.protocol}'`;
1422+
return msg;
1423+
}, Error);
14151424

14161425
// This should probably be a `TypeError`.
14171426
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
@@ -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
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) {
@@ -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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.