Repository navigation
fix: convert Windows absolute paths to file URLs in ESM resolve hook - #152
Merged
ilearnio merged 12 commits intoNov 1, 2026
Merged
Conversation
artnikbrothers
force-pushed
the
fix/windows-esm-file-url
branch
from
October 31, 2026 19:46
d20873d to
2722ff3
Compare
Short-circuiting the resolve hook skipped any loader registered before module-alias (e.g. tsx rewriting .js to .ts). Absolute targets are still converted to file: URLs, then handed to nextResolve. register.mjs and esm-loader.mjs now share one hook implementation.
index.mjs handed raw C:\ paths to nextResolve, which fails with ERR_UNSUPPORTED_ESM_URL_SCHEME. It now uses the shared resolve hook.
npm 3 on Node 6 cannot install the dev dependencies, so that job always failed.
On Node 22.15+ registerHooks also intercepts require(), and the CommonJS resolver cannot take file: URLs, so aliased require() calls broke. Specifiers with ?query or #hash were percent-encoded into the path.
registerHooks shipped in Node 22.15 and 23.5, so the version check broke register.mjs and index.mjs on 23.0 - 23.4. A named registerHooks import also made index.mjs fail to link on older Node, so its version warning never ran. reset() re-registered the hook on the next addAlias, stacking resolve hooks; it now only clears alias state.
path.join turned lodash-alias/fp into lodash\fp, which is not a valid package specifier.
artnikbrothers
force-pushed
the
fix/windows-esm-file-url
branch
from
October 31, 2026 20:23
f4f44cc to
3fbdab2
Compare
Merged
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
On Windows, aliased imports with
--import module-alias/registerfail withERR_UNSUPPORTED_ESM_URL_SCHEME ... Received protocol 'c:'. The resolve hooks pass the resolved path straight tonextResolve(), and Node parsesC:\...as a URL with protocolc:. On Linux/Mac it happens to work because/foo/baris also a valid URL, so the bug only shows on Windows.There are three resolve hooks:
register.mjs(sync, Node 22.15+),esm-loader.mjs(async, older Node) andindex.mjs(the programmaticaddAliasAPI, Node 22.15+). They now share one implementation inesm-loader.mjs. Absolute targets are converted withpathToFileURL()and then handed tonextResolve(). They no longer short-circuit, so loaders registered before module-alias (tsx and the like) still see the request. The first version of this PR short-circuited, and that broketsx --import module-alias/registeron every platform.Two more things the shared hook takes care of:
require()keeps the plain path. On Node 22.15+registerHooksalso intercepts CommonJS, and the CJS resolver can't takefile:URLs.?queryor#hashstays outside the encoded path, soimport('@lib/mod.mjs?v=2')still works./on Windows (path.joingavelodash\fp).register.mjsandindex.mjsnow checktypeof module.registerHooks === 'function'instead of the Node version. It shipped in 22.15 and 23.5, so 23.0 - 23.4 were broken.index.mjsalso used a namedregisterHooksimport, which madeimport 'module-alias'fail to link on Node < 22.15 before the version warning could run.reset()no longer re-registers the hook on the nextaddAlias, so hooks don't stack up.init()had a similarstartsWith('/')check for alias targets, replaced it withpath.isAbsolute()too.Tests: unit tests for the hook, plus integration fixtures for a loader chain (an earlier loader rewriting
.jsto.mjs), the programmatic API (includingreset()), loadingindex.mjson every Node version, andrequire()underregister.mjs(the registerHooks ones are skipped where it is missing). The integration tests passregister.mjsto--importas a file URL, they were hitting the samec:problem.CI: this is the first time the workflow runs against master at all (the triggers only listed dev). Added windows-latest, and the ESM job now runs Node 20, 22, 24 and 26. Node 6 is gone from the legacy job, npm 3 can't install the dev dependencies anymore, so it failed with or without this PR. The
NODE_ENV=testprefix in the test scripts breaks on Windows and nothing reads it, so I dropped it. Node 18 moved to the CJS-only job, mocha 2 loads.mjsthroughrequire()and that only works on Node 20.19+. The CJS suite stays Linux-only for now, two of its tests fail on Windows for reasons older than this PR.The bug exists in all versions that ship the ESM loader (2.3.0 - 2.3.4). Fixes #151