how to run `npm install` without using ` — legacy-peer-deps` flag?

Luis Rangel
2 min readAug 18, 2023

--

Running npm install with the --legacy-peer-deps flag is often used as a workaround when you encounter issues related to peer dependencies. However, the proper way to address these issues is by updating your project's dependencies to versions that are compatible with each other. Here's how you can do it:

  • Check for Updates: Before running npm install, check if there are updates available for your project's dependencies. Run:
npm outdatedb

This will show you which packages have newer versions available.

  • Update Package.json: Update your project’s package.json file to specify the versions of packages that you want to install. This helps in ensuring compatibility between packages. For example:
{
"dependencies": {
"package-name": "^1.2.3"
}
}

The ^ symbol indicates that npm can install versions greater than or equal to 1.2.3.

  • Remove node_modules and package-lock.json: If you have been using --legacy-peer-deps, you might have inconsistencies in your node_modules and package-lock.json files. Before proceeding, you might want to delete these files:
rm -rf node_modules package-lock.json
  • Run npm Install: After updating your package.json and removing the old node_modules and package-lock.json, you can run a fresh npm install:
npm install
  • Check Compatibility: During the installation process, npm will try to resolve dependencies and install compatible versions. If any conflicts or warnings arise, carefully read them and make adjustments accordingly.
  • Test Your Application: After installing the updated packages, thoroughly test your application to ensure everything works as expected.

Remember, using --legacy-peer-deps should be a temporary solution, and the best practice is to keep your dependencies up to date and compatible with each other to avoid potential issues in the future.

--

--

Luis Rangel
Luis Rangel

Written by Luis Rangel

Hi I’m Luis Rangel, a Full Stack Developer and a newbie on a loop 🚀 from Guatemala, currently, I’m a Team Member Telus International🙍🏽‍♂️ @luisrangelc.

Responses (1)