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.
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 ignoredCommon 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
module.exports = {
transformIgnorePatterns: [
'node_modules/(?!(?:\\.pnpm/)?(@scope/ui|other-esm)/)',
],
};Confirm the exact failing path
- Read the path in the stack trace - match your regex against that literal string.
- For pnpm, include the
.pnpm/store segment in the alternation. - Re-run with
--no-cacheso an old transform result is not reused.
How to prevent it
- Test the regex against the real
node_modulespath, not the package name alone. - Account for pnpm/Yarn store layouts in the pattern.
- Clear the Jest cache when changing transform config.