Repository navigation
|
Hello, for a long time now i have a project which is CJS and is unlikely to switch in the near future, but the dependencies have long since moved on to ESM, requiring us to stay on old versions. But regardless of what i try, i cannot get it to work and only encounter the following error: Must use import to load ES Module: /mnt/ssd/projects/nodejs/jest-esm/node_modules/camelcase/index.js
The file contains ESM syntax (import/export) that could not be executed as CommonJS. Either:
- Configure a transform (e.g. babel-jest) that compiles this file to CommonJS (see https://jestjs.io/docs/code-transformation)
- If the file is in "node_modules", allow it to be transformed by adjusting "transformIgnorePatterns" (see https://jestjs.io/docs/configuration#transformignorepatterns-arraystring)
- Use Node v24.9+ where Jest supports require(esm) natively (see https://jestjs.io/docs/ecmascript-modules#require-of-esm)
> 1 | const {default: camelcase} = require("camelcase")
| ^
2 |
3 | module.exports = function test() {
4 | return camelcase("TEST TEST");
at createRequireEsmError (node_modules/jest-runtime/build/index.js:452:17)
at Object.require (src/index.js:1:30)
at Object.require (test/testy.test.js:1:14)I have also seen (in some issues) that the modules may require Am i doing something wrong, or is the documentation just misleading? Jest 30.5.1 |
Replies: 1 comment 4 replies
|
Hi @hasezoey Yes, Your reproduction currently has: "scripts": {
"test": "jest"
}Change that to: "scripts": {
"test": "node --experimental-vm-modules ./node_modules/jest/bin/jest.js"
}or run Jest with: NODE_OPTIONS=--experimental-vm-modules npm testThe reason plain Node works while Jest does not is that Jest executes ESM through Node's So Node 24.9+ is required for Jest's synchronous Adding The documentation is therefore technically correct, although the Please mark this answer as accepted if it helped, thank you. |
Hi @hasezoey
Yes,
require(esm)is supported, but there is one additional requirement in Jest: Jest itself still needs to be started with Node's--experimental-vm-modulesflag.Your reproduction currently has:
Change that to:
or run Jest with:
NODE_OPTIONS=--experimental-vm-modules npm testThe reason plain Node works while Jest does not is that Jest executes ESM through Node's
vm.ModuleAPIs. Even on Node 26, those APIs are still gated behind--experimental-vm-modules.So Node 24.9+ is required for Jest's synchronous
require(esm)implementation, but it does not remove …