Skip to content
Latchkey

Jest ESM "must set --experimental-vm-modules" in CI

Jest runs tests inside Node's vm module. To execute native ES modules it needs --experimental-vm-modules on the Node process. Without it, Jest reports that import is not supported in its environment.

What this error means

Jest fails an ESM test file with a note that you must run Node with --experimental-vm-modules, or with "Cannot use import statement outside a module" inside the Jest environment.

jest
SyntaxError: Cannot use import statement outside a module

    Details:
    To use ECMAScript Modules with Jest, you need to run Node with the
    --experimental-vm-modules flag.

Common causes

ESM test files without the vm-modules flag

Jest's native ESM support depends on Node's experimental vm modules; the flag is not set, so import fails.

No transform configured for ESM/TypeScript

Without babel-jest or ts-jest ESM support, raw import reaches the vm and is rejected.

How to fix it

Enable the flag in the test script

Set NODE_OPTIONS so the Node process running Jest enables vm modules.

package.json
{
  "scripts": {
    "test": "NODE_OPTIONS=--experimental-vm-modules jest"
  }
}

Or transpile tests to the format Jest expects

Configure babel-jest or ts-jest so test files are compiled to a module format Jest can load.

jest.config.js
// jest.config.js
export default {
  transform: { '^.+\\.[tj]sx?$': 'babel-jest' }
};

How to prevent it

  • Set NODE_OPTIONS=--experimental-vm-modules for native-ESM Jest suites.
  • Configure a transform if you rely on compiled tests.
  • Keep the Jest and Node versions compatible with ESM support.

Related guides

Tired of flaky CI? Latchkey auto-heals failed jobs and retries them for you. Start free →