Jest cannot find babel-preset-env
Cannot find module 'babel-preset-env' from '/path/to/project'Short answer
Babel 7 moved its packages to the @babel scope, so the preset is now @babel/preset-env. Replace babel-preset-env or the old env shorthand in your Babel config, and install babel-jest, @babel/core and @babel/preset-env as dev dependencies.
Fix it fast
1Find out which cause you have
Find every place the old preset name is written.
grep -rn "preset-env\|\"env\"" .babelrc babel.config.js package.json2Match what you see, then jump to its fix
What does the error look like?
Cannot find module 'babel-preset-env' from '/path/to/project'Did you mean "@babel/env"?
Cause 1 of 2
The config still names the Babel 6 preset
1Check
The Babel 7 guide moved packages to the @babel scope by replacing the babel- prefix. Shorthand still works but must keep the scope.
cat .babelrc2Fix
Use the scoped name in presets and remove the old package.
// babel.config.js
module.exports = {
presets: [['@babel/preset-env', {targets: {node: 'current'}}]],
};3Verify
Run the tests again.
npx jestConfirmed by babeljs.io
Cause 2 of 2
The Babel packages Jest needs are not installed
1Check
Jest's guide installs three packages together: babel-jest, @babel/core and @babel/preset-env.
npm ls babel-jest @babel/core @babel/preset-env2Fix
Install them as dev dependencies.
npm install --save-dev babel-jest @babel/core @babel/preset-env3Verify
The listing shows all three, and the tests run.
npm ls babel-jest @babel/core @babel/preset-env
npx jestConfirmed by jestjs.io
How do I stop it coming back?
- Copy Babel setup from the current Jest guide, not from old posts.
- Keep Babel config in one file.
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 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 fixBack end
The command npm run build -- --prod exited with code 1 (Angular)
The command "npm run build -- --prod" exited with code 1
Exit code 1 only says the build failed; the real message is above it.
2 causes, each with a fix