Skip to content

Build with rolldown so that require() works - #38

Merged
NullVoxPopuli merged 1 commit into
handlebars-lang:masterfrom
NullVoxPopuli-ai-agent:nvp/fix-cjs-build
Oct 7, 2026
Merged

NullVoxPopuli merged 1 commit into
handlebars-lang:masterfrom
NullVoxPopuli-ai-agent:nvp/fix-cjs-build

Conversation

@NullVoxPopuli-ai-agent

Copy link
Copy Markdown

require('@handlebars/parser') works again. Fixes #33. This PR replaces #34.

Cause

The package has "type": "module", and the CommonJS build was dist/cjs/*.js. Node read those files as ES modules and threw:

ReferenceError: exports is not defined in ES module scope

Changes

  • rolldown replaces the two tsc builds. It writes dist/cjs/index.cjs and dist/esm/index.js.
  • main and the require and default export conditions point to dist/cjs/index.cjs.
  • spec/cjs.cjs is a new test. It loads the package by name with require. The test fails on master with the error above.
  • The CI test job builds on Node 22, then runs the tests on each Node version of the matrix.

Why #34 failed on Node 18

rolldown imports styleText from node:util. Node 18 does not have that export, so pnpm test stopped in the build step.

The package still supports Node 18. Only the build needs a newer Node.

Behavior changes

  • Each build is one file. Deep imports such as @handlebars/parser/dist/cjs/parse.js do not exist now. The exports map did not permit them before this PR.
  • tsc compiled to ES5. rolldown keeps the syntax of the source, which is ES2015 and later.

Checks

  • 110 tests pass on Node 18.20, 20.19, 22.22, and 24.20, with the build from Node 22.
  • A project that installs the packed tarball can load the package with require and with import.
  • pnpm lint passes.

🤖 Generated with Claude Code

The package has "type": "module", and the CommonJS build was
dist/cjs/*.js. Node read those files as ES modules, and each
require('@handlebars/parser') threw "exports is not defined in ES
module scope".

rolldown now writes dist/cjs/index.cjs and dist/esm/index.js. The
"main" field and the "require" and "default" export conditions point
to the .cjs file.

rolldown does not run on Node 18. The CI test job builds on Node 22
and then runs the tests on each Node version of the matrix.

Fixes handlebars-lang#33

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
@NullVoxPopuli NullVoxPopuli added the bug Something isn't working label Oct 7, 2026
@NullVoxPopuli
NullVoxPopuli merged commit 6616343 into handlebars-lang:master Oct 7, 2026
5 of 6 checks passed
@github-actions github-actions Bot mentioned this pull request Oct 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

CJS build is unusable: dist/cjs/*.js are CommonJS but package "type":"module" makes Node parse them as ESM ("exports is not defined")

2 participants