Skip to content

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

Open
artnikbrothers wants to merge 6 commits into
ilearnio:masterfrom
artnikbrothers:fix/windows-esm-file-url
Open

fix: convert Windows absolute paths to file URLs in ESM resolve hook#152
artnikbrothers wants to merge 6 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.

The fix is the same in both hooks (the sync one in register.mjs for Node 22.15+, the async one in esm-loader.mjs for older Node): if the resolved path is absolute, convert it with pathToFileURL() first. init() had a similar startsWith('/') check for alias targets, replaced it with path.isAbsolute() too.

Added windows-latest to CI plus unit tests for the resolve hook. The integration tests now pass register.mjs to --import as a file URL, it was hitting the same c: problem. Green on Windows with Node 20 and 22.

I also had to clean up CI a bit, because this is the first time the workflow runs against master at all (the triggers only listed dev, so master PRs never ran tests). 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. Same story with the Node 6 job, npm 3 simply can't install the dev dependencies anymore, it fails the same way without my changes.

The bug exists in all versions that ship the ESM loader (2.3.0 - 2.3.4). Fixes #151

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant