Jest ESM "Cannot use import" - Configure Jest for ESM Projects
Your package is "type": "module", so Node treats files as ESM, but Jest defaults to CommonJS. The mismatch shows up as Cannot use import statement or, conversely, module is not defined in ES module scope when a CommonJS config is loaded as ESM.
What this error means
In an ESM-first repo Jest either rejects import syntax or fails to read its own jest.config.js with "module is not defined in ES module scope." The project runs fine under Node directly, but Jest’s CommonJS assumptions break.
ReferenceError: module is not defined in ES module scope
This file is being treated as an ES module because it has a '.js'
file extension and '/app/package.json' contains "type": "module".Common causes
CommonJS config in an ESM package
With "type": "module", a jest.config.js using module.exports is parsed as ESM and module is undefined. The config itself fails to load.
Native ESM not enabled for the runner
Jest needs --experimental-vm-modules (and an ESM-aware transform) to run import natively. Without it, ESM test files are parsed as CommonJS and import throws.
How to fix it
Enable native ESM for Jest
Run Node with the VM-modules flag so Jest can execute ESM, and use an ESM transform preset.
NODE_OPTIONS=--experimental-vm-modules npx jest
# package.json
"scripts": { "test": "NODE_OPTIONS=--experimental-vm-modules jest" }Use the right config file extension
In an ESM package, write the config as ESM (export default) or rename it to .cjs so it loads as CommonJS.
// jest.config.mjs (ESM)
export default { transform: {} };
// or jest.config.cjs (CommonJS, opt out of ESM parsing)
module.exports = { transform: {} };How to prevent it
- Pick one module system for the repo and configure Jest to match.
- Name config
.cjs/.mjsexplicitly when"type"would otherwise misparse it. - Consider Vitest for projects that are ESM-first end to end.