esm: avoid throw when module specifier is not url

This particular exception is responsible for a lot of overhead when
debugging with large esm codebases with VS Code and break on caught
exceptions is enabled.

VS Code silently suppresses this exception, but the mechanism it uses
to do so is a bit slow so avoiding this common exception can speed up
loading of esm code.

In my scenario this saved over have of the startup run time
(over 20 seconds).
This should also make debugging without suppression of this exception
more pleasant in other tools such as the Chrome dev tools when
attached to NodeJs processes.

PR-URL: https://github.com/nodejs/node/pull/61000
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: Jacob Smith <jacob@frende.me>
Reviewed-By: Geoffrey Booth <webadmin@geoffreybooth.com>
This commit is contained in:
Craig Macomber (Microsoft)
2025-12-30 10:15:52 -08:00
committed by GitHub
parent 20bf3287c2
commit 06bf489379

View File

@@ -42,6 +42,7 @@ const {
ERR_INVALID_MODULE_SPECIFIER,
ERR_INVALID_PACKAGE_CONFIG,
ERR_INVALID_PACKAGE_TARGET,
ERR_INVALID_URL,
ERR_MODULE_NOT_FOUND,
ERR_PACKAGE_IMPORT_NOT_DEFINED,
ERR_PACKAGE_PATH_NOT_EXPORTED,
@@ -846,12 +847,13 @@ function moduleResolve(specifier, base, conditions, preserveSymlinks) {
} else if (protocol === 'file:' && specifier[0] === '#') {
resolved = packageImportsResolve(specifier, base, conditions);
} else {
try {
resolved = new URL(specifier);
} catch (cause) {
const url = URLParse(specifier);
if (url !== null) {
resolved = url;
} else {
if (isData && !BuiltinModule.canBeRequiredWithoutScheme(specifier)) {
const error = new ERR_UNSUPPORTED_RESOLVE_REQUEST(specifier, base);
setOwnProperty(error, 'cause', cause);
setOwnProperty(error, 'cause', new ERR_INVALID_URL(specifier));
throw error;
}
resolved = packageResolve(specifier, base, conditions);