Skip to content
Latchkey

Jest transformIgnorePatterns Not Excluding an ESM Dependency

You added a package to transformIgnorePatterns but Jest still throws on its ESM syntax. The negative-lookahead regex is almost always slightly wrong - a scoped name, a nested hoist, or a missing alternation makes the pattern fail to match.

What this error means

Despite a transformIgnorePatterns: ["/node_modules/(?!pkg)"] entry, Jest still reports an unexpected token inside that package. The regex looks right but does not actually exclude the file Jest is failing on.

Jest output
SyntaxError: Unexpected token 'export'

  at node_modules/.pnpm/@scope+ui@2.0.0/node_modules/@scope/ui/index.js
  // transformIgnorePatterns: ['/node_modules/(?!@scope/ui)']  -> still ignored

Common causes

Scoped package name not escaped/grouped

A scoped package like @scope/ui needs the / matched literally and the group anchored correctly. (?!@scope/ui) can miss the path when the slash is consumed elsewhere.

Nested or hoisted copy under a store path

pnpm/Yarn store the package under .pnpm/... or a nested node_modules, so a pattern anchored only at the top-level node_modules/ does not match the real file path.

How to fix it

Write a pattern that matches every install layout

Allow-list multiple packages and account for nested node_modules with a non-greedy prefix.

jest.config.js
// jest.config.js
module.exports = {
  transformIgnorePatterns: [
    'node_modules/(?!(?:\\.pnpm/)?(@scope/ui|other-esm)/)',
  ],
};

Confirm the exact failing path

  1. Read the path in the stack trace - match your regex against that literal string.
  2. For pnpm, include the .pnpm/ store segment in the alternation.
  3. Re-run with --no-cache so an old transform result is not reused.

How to prevent it

  • Test the regex against the real node_modules path, not the package name alone.
  • Account for pnpm/Yarn store layouts in the pattern.
  • Clear the Jest cache when changing transform config.

Frequently asked questions

What causes "transformIgnorePatterns mis-set"?
A scoped package like @scope/ui needs the / matched literally and the group anchored correctly. (?!@scope/ui) can miss the path when the slash is consumed elsewhere.
How do I fix transformIgnorePatterns mis-set?
Allow-list multiple packages and account for nested node_modules with a non-greedy prefix.

Related guides

References

Latchkey auto-heals failures like this one - detected, fixed, and retried without you. Start free → 30-day trial · No credit card