Repository navigation
fix(npm): Route bare "zlib" imports through the ESM namespace shim - #1357
Conversation
The zlib-namespace-shim plugin only matched the "node:zlib" specifier, but bundled dependencies (binpatch) import from bare "zlib". esbuild hoisted those into top-level named imports of the version-gated zstd* family (added in Node 22.15), which fail at link time on older Node — the entire ESM entry of the npm package failed to load on Node 18/20, despite engines.node claiming >= 18. Widen the filter to match both specifiers, and add a regression test that statically scans the built ESM bundle for link-fatal named zlib imports. (The existing dynamic import test only runs on the build Node, which already has zstd and can never catch this.) Verified: dist/index.mjs now imports cleanly on Node 18.20.8 and 20.19.5; CJS unaffected. Co-Authored-By: Claude <noreply@anthropic.com>
There was a problem hiding this comment.
verified the fix end-to-end. rebuilt dist/index.mjs with the old narrow /^node:zlib$/ filter and it does emit the link-fatal import{zstdDecompressSync,createZstdDecompress}from"zlib" — bundled binpatch imports from bare zlib, so esbuild hoisted the version-gated zstd names past the shim. widening to /^(?:node:)?zlib$/ routes those through the namespace shim: rebuilt bundle now has exactly one import * as ... from "node:zlib" and no named zstd import, and loads cleanly.
the regression test is real coverage, not decoration — it fails against the pre-fix bundle and passes against the fixed one. CJS path unaffected (shim is ESM-only). biome clean.
one nit, non-blocking: engines.node is >=20.0 but the PR body/comments mention Node 18 — the zstd gate is 22.15 either way so it doesn't affect the fix, just the prose.
The zlib-namespace-shim plugin only matched the "node:zlib" specifier, but bundled dependencies (binpatch) import from bare "zlib". esbuild hoisted those into top-level named imports of the version-gated zstd* family (added in Node 22.15), which fail at link time on older Node — the entire ESM entry of the npm package failed to load on Node 18/20, despite engines.node claiming >= 18.
Widen the filter to match both specifiers, and add a regression test that statically scans the built ESM bundle for link-fatal named zlib imports. (The existing dynamic import test only runs on the build Node, which already has zstd and can never catch this.)
Verified: dist/index.mjs now imports cleanly on Node 18.20.8 and 20.19.5; CJS unaffected.