Skip to content
Latchkey

Node --experimental-vm-modules Required for ESM Tests in CI - Enable the Flag

Jest evaluates ESM through the Node VM modules API, which is gated behind --experimental-vm-modules. Without the flag, ESM test files fail to load.

What this error means

Running ESM Jest tests in CI fails with a message telling you to run Node with --experimental-vm-modules, and import statements in test files throw before any assertion runs.

node
To enable ECMAScript modules, you must run Node with the
--experimental-vm-modules flag.

SyntaxError: Cannot use import statement outside a module

Common causes

Jest ESM support requires the VM modules flag

Jest uses vm.Module to evaluate ES modules, an API still flagged as experimental in Node.

The flag is set locally but not in CI

A developer alias or shell rc adds the flag, but the CI command runs plain jest without it.

How to fix it

Pass the flag via NODE_OPTIONS

  1. Set NODE_OPTIONS for the test step so Jest can load ESM.
  2. Run the test command unchanged.
GitHub Actions
- run: jest
  env:
    NODE_OPTIONS: --experimental-vm-modules

How to prevent it

  • Define the test script with the flag baked in (cross-env NODE_OPTIONS) so CI and local runs use the identical command and ESM test loading never depends on a shell alias.

Related guides

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