Skip to content

Load dist/cjs as CommonJS, and smoke-test the packed entry points in CI - #125

Open
roger-datocms wants to merge 4 commits into
datocms:masterfrom
roger-datocms:fix/cjs-type-commonjs
Open

roger-datocms wants to merge 4 commits into
datocms:masterfrom
roger-datocms:fix/cjs-type-commonjs

Conversation

@roger-datocms

@roger-datocms roger-datocms commented Nov 16, 2026 •

Copy link
Copy Markdown
Member

(Human here)

This is actually just a one-line fix, a nested package.json with: "type": "commonjs" for the CJS build.

The rest is just test stuff and a Node script to copy the package.json into the build after Typescript compilation. This repo doesn't have tsup etc. so we have to do it manually.


(Claude here [Opus 5.5])

Fixes #124.

Since v4.1.1, the root "type": "module" has made Node parse the CommonJS build in dist/cjs as ESM, so require("react-datocms") and every require of a subpath throw ReferenceError: exports is not defined in ES module scope. import and bundled apps were never affected.

Changes

  • After tsc:cjs, scripts/copy-cjs-package-json.mjs copies scripts/cjs-package.json ({"type": "commonjs"}) to dist/cjs/package.json. Node uses the nearest package.json to decide a .js file's format, so that folder loads as CommonJS again. This is the same marker tshy writes into its CommonJS build.
  • New npm run test:package (scripts/test-package.mjs): builds, packs the tarball, installs it into a throwaway consumer, and loads every exports entry point with require and import. It reads the entry points from package.json, so new subpaths are covered automatically.
  • New package CI job runs it on the .nvmrc Node version. The existing job only runs npm test against src, which is how this shipped unnoticed.
  • Patch changeset.

Before / after

node scripts/test-package.mjs on Node 24.12.0:

Before (marker file removed):

FAIL require react-datocms/seo: ReferenceError: exports is not defined in ES module scope
ok   import react-datocms/seo
...
Node v24.12.0: 12/24 entry point loads succeeded   (exit 1)

After:

ok   require react-datocms/seo
ok   import react-datocms/seo
...
Node v24.12.0: 24/24 entry point loads succeeded   (exit 0)

npm test: 6 suites, 54 tests, 42 snapshots pass.

Checked for regressions

Comparing packed tarballs with and without the fix: the import path is unchanged. The dist/esm and dist/types folders are identical, and built output is byte-identical for Next.js 16 (Turbopack and webpack), Vite (examples/), webpack, esbuild and rollup. TypeScript resolution results are the same under node16, nodenext, bundler and node10.

Known limits (not regressions)

  • On Node below 20.19 / 22.12, require of the root and /video-player still fails, because @mux/mux-player-react/lazy only ships ESM and those versions can't require() ESM. Every supported Node line handles it.
  • CommonJS TypeScript consumers on module: node16/nodenext still get TS1479, because the types only live in the ESM-flagged dist/types.

TODO later

  • Move the CI test matrix from Node 16/18/20 (all end-of-life) to 22/24/26.
  • Narrow engines from >=8.0.0 to what actually works (e.g. >=20.19), in the next major.
  • In the next major, drop the marker file: either emit .cjs files with tsup/tsdown (as @datocms/content-link does) or ship ESM only, now that every supported Node can require() ESM.

🤖 Generated with Claude Code

roger-datocms and others added 4 commits November 16, 2026 11:10
The root "type": "module" made Node parse dist/cjs as ESM, so every
`require` entry point threw. A nested dist/cjs/package.json with
"type": "commonjs" fixes it. The new CI job packs the library and loads
every entry point with `require` and `import`.

Fixes datocms#124

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

dist/cjs is loaded as ESM because of "type": "module", so all require exports fail

1 participant