Skip to content

fix: convert Windows absolute paths to file URLs in ESM resolve hook - #152

Merged
ilearnio merged 12 commits into
ilearnio:masterfrom
artnikbrothers:fix/windows-esm-file-url
Nov 1, 2026
Merged

ilearnio merged 12 commits into
ilearnio:masterfrom
artnikbrothers:fix/windows-esm-file-url

Conversation

@artnikbrothers

@artnikbrothers artnikbrothers commented Aug 19, 2026 •

Copy link
Copy Markdown
Contributor

On Windows, aliased imports with --import module-alias/register fail with ERR_UNSUPPORTED_ESM_URL_SCHEME ... Received protocol 'c:'. The resolve hooks pass the resolved path straight to nextResolve(), and Node parses C:\... as a URL with protocol c:. On Linux/Mac it happens to work because /foo/bar is 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) and index.mjs (the programmatic addAlias API, Node 22.15+). They now share one implementation in esm-loader.mjs. Absolute targets are converted with pathToFileURL() and then handed to nextResolve(). 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 broke tsx --import module-alias/register on every platform.

Two more things the shared hook takes care of:

  • require() keeps the plain path. On Node 22.15+ registerHooks also intercepts CommonJS, and the CJS resolver can't take file: URLs.
  • A ?query or #hash stays outside the encoded path, so import('@lib/mod.mjs?v=2') still works.
  • Bare specifiers built from alias targets use / on Windows (path.join gave lodash\fp).

register.mjs and index.mjs now check typeof module.registerHooks === 'function' instead of the Node version. It shipped in 22.15 and 23.5, so 23.0 - 23.4 were broken. index.mjs also used a named registerHooks import, which made import 'module-alias' fail to link on Node < 22.15 before the version warning could run. reset() no longer re-registers the hook on the next addAlias, so hooks don't stack up.

init() had a similar startsWith('/') check for alias targets, replaced it with path.isAbsolute() too.

Tests: unit tests for the hook, plus integration fixtures for a loader chain (an earlier loader rewriting .js to .mjs), the programmatic API (including reset()), loading index.mjs on every Node version, and require() under register.mjs (the registerHooks ones are skipped where it is missing). The integration tests pass register.mjs to --import as a file URL, they were hitting the same c: 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=test prefix 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 .mjs through require() 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

@artnikbrothers
artnikbrothers force-pushed the fix/windows-esm-file-url branch from d20873d to 2722ff3 Compare October 31, 2026 19:46
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
artnikbrothers force-pushed the fix/windows-esm-file-url branch from f4f44cc to 3fbdab2 Compare October 31, 2026 20:23
@ilearnio
ilearnio merged commit a982c7b into ilearnio:master Nov 1, 2026
15 checks passed
ilearnio added a commit that referenced this pull request Nov 1, 2026
Fixes ESM aliases on Windows (#151, #152). Also replaces the dead Travis
badge in the README with the GitHub Actions badge.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
@ilearnio ilearnio mentioned this pull request Nov 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

2 participants