Jest cannot resolve imports that use an alias or a src-relative path
Cannot 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.
Fix it fast
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"'2Match what you see, then jump to its fix
- 1
Cannot find module '@/...' while the app builds fineAn alias like @/ has no mapping in the Jest configFix - 2
Cannot find module 'components/...' with no alias prefixA bare path like components/Button relies on a baseUrlFix - 3
The mapping exists but the import still lands in the wrong folder or fileA broader pattern catches the import firstFix
- 1
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.json2Fix
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.jsConfirmed by jestjs.io
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.json2Fix
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.jsConfirmed by jestjs.io
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 moduleNameMapper2Fix
Anchor every pattern and put the narrow ones first.
moduleNameMapper: {
'^@/components/(.*)$': '<rootDir>/src/ui/$1',
'^@/(.*)$': '<rootDir>/src/$1',
},3Verify
Run the whole suite.
npx jestConfirmed by jestjs.io
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
Node.js says Cannot find module when you run or require a file
Error: Cannot find module './routes/users'
Node.js looked for the name you gave it in every place its resolver knows and found nothing.
4 causes, each with a fixBack 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 fixBack end
ng fails with Cannot find module @angular/cli/bin/ng
Cannot find module '@angular/cli/bin/ng'
The ng command is the entry point to the Angular CLI package.
2 causes, each with a fixBack end
Angular app shows 'Cannot GET /' when served by a Node.js server
Cannot GET /dashboard
Cannot GET / is Express saying no route or file matches the request.
3 causes, each with a fixBack 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 fixBack end
Angular build says it cannot resolve '@angular/localize/init'
Can't resolve '@angular/localize/init' in '/usr/src/app/src'
The build imports @angular/localize, and that package is not in node_modules.
2 causes, each with a fix