fix: serve CJS runtime with ESM type declarations#130
Draft
angeloashmore wants to merge 1 commit intomainfrom
Draft
fix: serve CJS runtime with ESM type declarations#130angeloashmore wants to merge 1 commit intomainfrom
angeloashmore wants to merge 1 commit intomainfrom
Conversation
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
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.
Resolves: #129, #105
Description
Fixes
ERR_MODULE_NOT_FOUNDerrors on Node.js 22+ and 24+ caused by ESM bare specifier imports ofnext/script,next/navigation, etc.The exports map now serves CJS for runtime (avoiding bare import issues) while pointing the
typescondition to.d.ts(ESM) declarations, ensuring TypeScript resolves@prismicio/clientandnext/*types correctly.Root cause and how this was tested
The package previously used
require/importconditional exports, serving.cjsfor CJS consumers and.js(ESM) for ESM consumers. The ESM output contains bare specifier imports likeimport Script from "next/script"which fail under strict ESM resolution in Node.js 22+/24+ because Next.js subpaths don't resolve without file extensions in ESM mode.Prior fix attempts (#106, #125, #128) oscillated between CJS-only and dual formats. CJS-only fixed runtime but created a type resolution problem:
.d.ctstype declarations resolve dependencies in CJS mode (requirecondition), and if a dependency like@prismicio/clientis ESM-only, TypeScript silently degrades those types toany(verified withtsc --traceResolution).The fix uses
types/defaultconditions instead ofrequire/import:require()andimport()resolve to.cjs— no bare import errors.typescondition →.d.ts→ resolves in ESM mode →@prismicio/clientandnext/*types resolve correctly with full type safety.This was tested by creating isolated test projects with
moduleResolution: "node16"and"bundler", verifying type resolution traces, and confirming that types do not silently degrade toany.Also set
exports: falseintsdown.config.tsto prevent tsdown from overwriting the custom exports map during builds.Checklist