Explain Codes LogoExplain Codes Logo

Fix the upstream dependency conflict installing NPM packages

javascript
npm
package-management
dependency-conflict
Anton ShumikhinbyAnton Shumikhin·Feb 7, 2025
TLDR

To fix a dependency conflict in NPM, run npm dedupe to correct duplicated dependencies or manually specify a version in package.json. Use this simple snippet to force a version:

"dependencies": { "conflicted-package": "x.x.x" }

For Yarn users, a similar solution is to add a resolutions block for version overrides:

"resolutions": { "conflicted-package": "x.x.x" }

This method makes the package manager use a specific version, thereby solving the conflict.

Understanding npm v7's peer dependency changes

npm v7 came with a change in handling peer dependencies which can cause conflicts during installation. The solution is running:

// NPM is "n" a "p"ain in the "m"aterial world? Try this! npm install --legacy-peer-deps

By running this command, npm reverts to the peer dependency resolution strategy from npm v6, which doesn't auto-install them.

Overcoming specific conflicts and dependencies

Forcing the issue

If --legacy-peer-deps doesn't resolve the issue, here's a potential showstopper:

// "Brute force: If it doesn't work, you're just not using enough." npm install --force

This overrides the package manager's usual safety checks.

Legacy everywhere

You can configure npm to use legacy peer dependencies for all installations:

npm config set legacy-peer-deps true

Manual meddling

If all else fails, you can manually specify the versions in package.json:

"overrides": { "mapbox-gl": "1.13.0" }

Preventing conflict disasters

To prevent the apocalypse before it happens, remember to:

  • Avoid the "*" wildcard in package versions
  • Brush up on the npm v7 changes
  • Use npx create-react-app with the --legacy-peer-deps flag for new React projects

When audit tools save the day

Occasionally, npm audit can be your saving grace, helping you identify and resolve where conflicts originate and even act as a security checker.

When things get complex

Handling edgy software

When using cutting-edge versions, you may need to take matters into your own hands and manage peer dependencies manually.

SSR-specific issues

Projects like Nuxt.js can encounter unique issues due to differences in the client and server-side environment. Better keep your eyes open!

Monorepo mayhem

When dealing with monorepos, consider using tools like Lerna or Yarn Workspaces to manage dependencies.