Last modified: Jul 02, 2026
Fix Cannot find module '@babel/preset-env'
If you are building a modern JavaScript application with Babel, you may encounter the error: Cannot find module '@babel/preset-env'. This error usually appears when you try to run your project or compile code using Babel, but the required preset is missing from your node_modules folder.
This article will guide you through the most common causes and solutions for this error. We will cover package installation, configuration fixes, and best practices. By the end, you will be able to resolve this error quickly and get back to coding.
What Causes the '@babel/preset-env' Error?
The error occurs because Babel cannot find the @babel/preset-env package in your project's dependencies. This preset is essential for compiling modern JavaScript (ES6+) into backward-compatible code. Common causes include:
- The package was never installed.
- The package was accidentally deleted or corrupted.
- You are running the command from the wrong directory.
- Your
package.jsonfile is missing the dependency entry.
Understanding these causes will help you apply the right fix. Let's start with the simplest solution.
Solution 1: Install @babel/preset-env as a Dev Dependency
The most common fix is to install the missing preset using npm or yarn. You should install it as a dev dependency because Babel presets are only needed during development and build processes.
Open your terminal in the root directory of your project and run:
npm install --save-dev @babel/preset-env
If you are using Yarn, use this command:
yarn add --dev @babel/preset-env
After installation, the package will be added to your package.json file under devDependencies. Verify it by checking the file:
{
"devDependencies": {
"@babel/preset-env": "^7.24.0"
}
}
Now try running your Babel command again. The error should be resolved.
Solution 2: Ensure You Are in the Correct Directory
Sometimes the error occurs because you are running the command from a different folder. Make sure your terminal is pointing to the project root where your package.json file exists.
You can check your current directory with:
pwd # On Linux/Mac
cd # On Windows
If you are in the wrong folder, navigate to your project root using the cd command. Then reinstall the preset as shown in Solution 1.
Solution 3: Delete node_modules and Reinstall All Dependencies
If the package is installed but the error persists, your node_modules folder may be corrupted. The safest fix is to delete the entire folder and reinstall all dependencies.
Run these commands sequentially:
rm -rf node_modules # On Linux/Mac
rmdir /s node_modules # On Windows (Command Prompt)
npm install
If you use Yarn, run yarn install instead of npm install. This will regenerate the node_modules folder with all the correct packages, including @babel/preset-env.
Solution 4: Check Your Babel Configuration File
Another reason for the error is a misconfigured Babel config file. Babel looks for a .babelrc, babel.config.js, or babel.config.json file in your project root. Ensure that your preset is listed correctly.
For example, a proper .babelrc file should look like this:
{
"presets": ["@babel/preset-env"]
}
If you are using a babel.config.js file, it should export the configuration:
// babel.config.js
module.exports = {
presets: ['@babel/preset-env']
};
Make sure there are no typos or missing quotes. The preset name must be exactly @babel/preset-env.
Solution 5: Use the Correct Version of Node.js
Sometimes the error can be related to an outdated Node.js version. @babel/preset-env works best with Node.js 12 or higher. Check your Node.js version with:
node -v
If your version is older, consider upgrading to the latest LTS version from the official Node.js website. After upgrading, reinstall your dependencies as shown in Solution 3.
Solution 6: Check for Global vs Local Installation
Babel presets should be installed locally in your project, not globally. If you accidentally installed @babel/preset-env globally, it may not be accessible to your project.
To verify, run:
npm list -g @babel/preset-env
If it shows a global installation, remove it with:
npm uninstall -g @babel/preset-env
Then install it locally as described in Solution 1.
Example: Running Babel After Fix
Once you have installed the preset correctly, you can test it with a simple JavaScript file. Create a file called test.js with modern syntax:
// test.js
const greet = (name) => {
return `Hello, ${name}!`;
};
console.log(greet('World'));
Now run Babel to compile it:
npx babel test.js --presets=@babel/preset-env
You should see the compiled output without errors:
"use strict";
var greet = function greet(name) {
return "Hello, ".concat(name, "!");
};
console.log(greet('World'));
If you see this output, the error is fixed.
Preventing the Error in the Future
To avoid this error in future projects, follow these best practices:
- Always install Babel presets as dev dependencies with
--save-dev. - Keep your
package.jsonand lock file (package-lock.jsonoryarn.lock) in version control. - Run
npm installafter cloning a project to ensure all dependencies are present. - Use a consistent Node.js version across your team with a
.nvmrcfile.
For related issues, check out our guide on Fix Node.js Error: Cannot find module for more troubleshooting tips.
Conclusion
The Cannot find module '@babel/preset-env' error is common but easy to fix. The most reliable solution is to install the preset as a dev dependency using npm or yarn. If the error persists, check your directory, reinstall dependencies, or verify your Babel configuration.
By following the steps in this article, you can resolve the error in minutes. Remember to keep your dependencies updated and your configuration files correct. With these practices, you will spend less time debugging and more time building great applications.
For more troubleshooting advice, revisit our article on Fix Node.js Error: Cannot find module for additional error scenarios.