Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

esm: improve error message of ERR_UNSUPPORTED_ESM_URL_SCHEME #34795

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions lib/internal/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ const {
WeakMap,
} = primordials;

const isWindows = process.platform === 'win32';

const messages = new Map();
const codes = {};

Expand Down Expand Up @@ -1429,8 +1431,15 @@ E('ERR_UNKNOWN_MODULE_FORMAT', 'Unknown module format: %s', RangeError);
E('ERR_UNKNOWN_SIGNAL', 'Unknown signal: %s', TypeError);
E('ERR_UNSUPPORTED_DIR_IMPORT', "Directory import '%s' is not supported " +
'resolving ES modules imported from %s', Error);
E('ERR_UNSUPPORTED_ESM_URL_SCHEME', 'Only file and data URLs are supported ' +
'by the default ESM loader', Error);
E('ERR_UNSUPPORTED_ESM_URL_SCHEME', (url) => {
let msg = 'Only file and data URLs are supported by the default ESM loader';
if (isWindows && url.protocol.length === 2) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why would the protocol length of two be what triggers this? Wouldn't it at the very least be > 1?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.protocol includes the :

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, so URL('C:\\example\\foo.mjs') would have .protocol === 'c:'. This covers only such use-case for now.

msg += '. Absolute Windows paths without prefix are not valid URLs, ' +
"consider using 'file://' prefix";
}
msg += `. Received protocol '${url.protocol}'`;
return msg;
}, Error);

E('ERR_V8BREAKITERATOR',
'Full ICU data not installed. See https://github.com/nodejs/node/wiki/Intl',
Expand Down
2 changes: 1 addition & 1 deletion lib/internal/modules/esm/resolve.js
Original file line number Diff line number Diff line change
Expand Up @@ -747,7 +747,7 @@ function defaultResolve(specifier, context = {}, defaultResolveUnused) {
if (parsed && parsed.protocol === 'nodejs:')
return { url: specifier };
if (parsed && parsed.protocol !== 'file:' && parsed.protocol !== 'data:')
throw new ERR_UNSUPPORTED_ESM_URL_SCHEME();
throw new ERR_UNSUPPORTED_ESM_URL_SCHEME(parsed);
if (NativeModule.canBeRequiredByUsers(specifier)) {
return {
url: 'nodejs:' + specifier
Expand Down
23 changes: 14 additions & 9 deletions test/es-module/test-esm-dynamic-import.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,11 @@ const absolutePath = require.resolve('../fixtures/es-modules/test-esm-ok.mjs');
const targetURL = new URL('file:///');
targetURL.pathname = absolutePath;

function expectErrorProperty(result, propertyKey, value) {
Promise.resolve(result)
.catch(common.mustCall((error) => {
assert.strictEqual(error[propertyKey], value);
}));
}

function expectModuleError(result, err) {
expectErrorProperty(result, 'code', err);
function expectModuleError(result, code, message) {
Promise.resolve(result).catch(common.mustCall((error) => {
assert.strictEqual(error.code, code);
if (message) assert.strictEqual(error.message, message);
}));
}

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