This repository provides a minimal reproduction for a regression introduced in @unhead/react (and @unhead/schema) version 2.0.18.
Starting with v2.0.18, a dynamic import() was introduced in transformHtmlTemplate to lazily load the parser module. See: transformHtmlTemplate.ts (v2.0.18)
In Node.js environments running in CommonJS mode (e.g., standard Jest tests) where the transpiler (Babel/SWC) is configured to preserve dynamic imports, this results in a crash:
TypeError: A dynamic import callback was invoked without --experimental-vm-modules
- CJS Context: Node.js requires special flags or ESM-first setup to handle
import()in a CJS context. - Preserved Imports: If your build pipeline targets modern Node or explicitly excludes dynamic import transpilation (e.g., to keep Webpack chunking working natively), the
import()call survives and hits the Node/Jest runtime environment.
Run the included shell script which handles the version swapping and testing:
yarn install
bash reproduce.sh# Verify v2.0.17 WORKS
yarn add @unhead/react@2.0.17 unhead@2.0.17 --silent
yarn jest repro.test.js --no-cache
# Verify v2.0.18 FAILS
yarn add @unhead/react@2.0.18 unhead@2.0.18 --silent
yarn jest repro.test.js --no-cache- Node: 22.x (or any version requiring flags for CJS dynamic imports)
- Jest: 30.x (using standard
babel-jest) - Babel: Configured with
exclude: ["proposal-dynamic-import"]to ensureimport()is preserved.
For users experiencing this, the current workarounds are:
- Downgrade to
2.0.17. - Transpile dynamic imports to
require()in your test environment usingbabel-plugin-dynamic-import-node. - Enable
--experimental-vm-modulesin Node (requires ESM setup in Jest).