From 06bf489379c9e5cb20f7d3a5e31ed1cf8cc0672c Mon Sep 17 00:00:00 2001 From: "Craig Macomber (Microsoft)" <42876482+CraigMacomber@users.noreply.github.com> Date: Tue, 30 Dec 2025 10:15:52 -0800 Subject: [PATCH] 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 Reviewed-By: Jacob Smith Reviewed-By: Geoffrey Booth --- lib/internal/modules/esm/resolve.js | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/lib/internal/modules/esm/resolve.js b/lib/internal/modules/esm/resolve.js index a321744ab8..cc12306488 100644 --- a/lib/internal/modules/esm/resolve.js +++ b/lib/internal/modules/esm/resolve.js @@ -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);