fix(path-equal): serve CJS type declarations to CJS consumers - #248
Merged
Conversation
The `exports` map carried a single top-level `types` pointing at
`./esm/index.d.ts` and no `require` condition, so a CommonJS consumer
resolving under `node16`/`nodenext` was handed the ESM declarations and told
the package was ESM:
TS1479: The current file is a CommonJS module whose imports will produce
'require' calls; however, the referenced file is an ECMAScript module
Nothing was wrong with the emitted code. `cjs/package.json` already declares
`{"type":"commonjs"}` and `cjs/index.d.ts` already shipped — the manifest
just never pointed at it. The two declaration files are byte-identical in
content and differ only in the `package.json` beside them, which is exactly
why the condition needs its own `types` rather than one shared entry.
`moduleResolution: "node"` ignores `exports` maps outright, which is why this
survived: it only surfaces once a consumer moves to modern resolution.
`exports` now resolves `require` and `import` separately, each pointing at
the declarations matching its format, and the top-level `types` agrees with
`main` instead of contradicting it.
Verified against the consumer that found it: `iso-path` builds clean with
`module`/`moduleResolution: node16`, where it failed before.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0163FWV18YYf6AVN3qAd4fYi
🦋 Changeset detectedLatest commit: e2cb9c7 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
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.
A CommonJS consumer resolving this package under
node16/nodenextis told it is ESM:The cause is the manifest, not the build
One top-level
types, and norequirecondition. A CJS consumer gets./cjs/index.jsfor code but./esm/index.d.tsfor types — so TypeScript reads the package as ESM and refuses therequire.Nothing is wrong with the emitted output.
cjs/package.jsonalready declares{"type":"commonjs"}andcjs/index.d.tsalready ships; the manifest simply never pointed at it. The two declaration files are byte-identical in content — what makes one CJS and the other ESM is thepackage.jsonbeside each, which is precisely why the condition needs its owntypesrather than one shared entry.This survived because
moduleResolution: "node"ignoresexportsmaps entirely. It only appears once a consumer moves to modern resolution.The fix
The top-level
typesalso moves to./cjs/index.d.tsso it agrees withmain(./cjs/index.js) instead of contradicting it, for consumers on legacy resolution that never readexports.Verification
Found by
iso-path, which imports this package and could not adoptmoduleResolution: node16because of it. Against this manifest,iso-pathbuilds clean —TS1479gone.path-equal's own tests pass unchanged: 6 passed.Patch changeset included. The sibling
uni-pathis ESM-only and makes no CJS claim, so it needs no equivalent change.🤖 Generated with Claude Code
https://claude.ai/code/session_0163FWV18YYf6AVN3qAd4fYi