Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

Jest cannot resolve imports that use an alias or a src-relative path

ErrorCannot find module '@/components/Button'

Short answer

Jest does not read the path settings of your bundler or tsconfig. An alias such as @/components, or a bare path such as components/Button, has to be declared in the Jest config. Map aliases with moduleNameMapper, use modulePaths for bare paths, and keep the most specific pattern first.

  • Applies to Jest 29, Jest 30, Node.js 22
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Let Jest show the config it actually uses, then look at moduleNameMapper, modulePaths and roots.

    npx jest --showConfig | grep -A8 -E 'moduleNameMapper|modulePaths|"roots"'
  2. 2Match what you see, then jump to its fix

    1. 1Cannot find module '@/...' while the app builds fineAn alias like @/ has no mapping in the Jest configFix
    2. 2Cannot find module 'components/...' with no alias prefixA bare path like components/Button relies on a baseUrlFix
    3. 3The mapping exists but the import still lands in the wrong folder or fileA broader pattern catches the import firstFix

What does the error look like?

  • Cannot find module '@/components/Button'
  • Cannot find module 'components/Button'

Cause 1 of 3

An alias like @/ has no mapping in the Jest config

1Check

moduleNameMapper maps regular expressions to paths. Without an entry for your alias, Jest looks for a package called @ in node_modules.

grep -n -A6 "moduleNameMapper" jest.config.* package.json

2Fix

Add a pattern for the alias and use the captured group in the target. Use <rootDir> for file paths.

// jest.config.js
module.exports = {
  moduleNameMapper: {
    '^@/(.*)$': '<rootDir>/src/$1',
  },
};

3Verify

Run the failing test file.

npx jest src/App.test.js

Confirmed by jestjs.io

Did this fix it?

Cause 2 of 3

A bare path like components/Button relies on a baseUrl

1Check

Imports without a prefix are searched in node_modules folders. A baseUrl in tsconfig or jsconfig is not read by Jest.

grep -n "baseUrl" tsconfig.json jsconfig.json

2Fix

List the folder in modulePaths. The Jest docs advise against adding '.' to moduleDirectories and recommend moduleNameMapper in most cases.

// jest.config.js
module.exports = {
  modulePaths: ['<rootDir>/src'],
};

3Verify

Run the failing test again.

npx jest src/App.test.js

Confirmed by jestjs.io

Did this fix it?

Cause 3 of 3

A broader pattern catches the import first

1Check

Patterns are checked one by one until one fits, and the docs say to list the most specific rule first. A pattern without ^ and $ also matches part of a longer name.

npx jest --showConfig | grep -A12 moduleNameMapper

2Fix

Anchor every pattern and put the narrow ones first.

moduleNameMapper: {
  '^@/components/(.*)$': '<rootDir>/src/ui/$1',
  '^@/(.*)$': '<rootDir>/src/$1',
},

3Verify

Run the whole suite.

npx jest

Confirmed by jestjs.io

Did this fix it?

How do I stop it coming back?

  • Keep the alias list in one place and copy it to the bundler, tsconfig and Jest together.
  • Anchor every moduleNameMapper pattern with ^ and $.
  • Check the mapping with --showConfig after every change.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Back end

ng serve says it cannot determine the project or target

Cannot determine project for command.

The Angular CLI looked through angular.json for a project that defines the target you asked for (serve, build, test) and found none, or found several and could not pick one.

3 causes, each with a fix

Back end

ng serve cannot find the dev-server builder package

Could not find the implementation for builder @angular/build:dev-server

The builder named in angular.json (for example @angular-devkit/build-angular:dev-server) is looked up as an npm package starting from the workspace root.

3 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.