[NPM] Fix Conflicting peer dependency error

[NPM] Fix Conflicting peer dependency error

2023-08-25 hit count image

Let's see how to fix the Conflicting peer dependency error when installing a package by NPM.

Outline

When developing a project with NodeJS, it is essential to install the required packages using NPM. In this blog post, I wll introduce how to fix the Conflicting peer dependency error that occurs when installing a package using NPM.

Conflicting peer dependency error

When installing a package using the npm install command, the Conflicting peer dependency error may occur as follows.

Installing @typescript-eslint/eslint-plugin@latest, eslint-plugin-react@latest, @typescript-eslint/parser@latest
npm ERR! code ERESOLVE
npm ERR! ERESOLVE could not resolve
npm ERR!
npm ERR! While resolving: [email protected]
npm ERR! Found: @typescript-eslint/[email protected]
npm ERR! node_modules/@typescript-eslint/eslint-plugin
npm ERR!   @typescript-eslint/eslint-plugin@"^5.5.0" from [email protected]
npm ERR!   node_modules/eslint-config-react-app
npm ERR!     eslint-config-react-app@"^7.0.1" from [email protected]
npm ERR!     node_modules/react-scripts
npm ERR!       react-scripts@"5.0.1" from the root project
npm ERR!   peerOptional @typescript-eslint/eslint-plugin@"^4.0.0 || ^5.0.0" from [email protected]
npm ERR!   node_modules/eslint-plugin-jest
npm ERR!     eslint-plugin-jest@"^25.3.0" from [email protected]
npm ERR!     node_modules/eslint-config-react-app
npm ERR!       eslint-config-react-app@"^7.0.1" from [email protected]
npm ERR!       node_modules/react-scripts
npm ERR!         react-scripts@"5.0.1" from the root project
npm ERR!   1 more (the root project)
npm ERR!
npm ERR! Could not resolve dependency:
npm ERR! dev @typescript-eslint/eslint-plugin@"6.4.1" from the root project
npm ERR!
npm ERR! Conflicting peer dependency: @typescript-eslint/[email protected]
npm ERR! node_modules/@typescript-eslint/parser
npm ERR!   peer @typescript-eslint/parser@"^6.0.0 || ^6.0.0-alpha" from @typescript-eslint/[email protected]
npm ERR!   node_modules/@typescript-eslint/eslint-plugin
npm ERR!     dev @typescript-eslint/eslint-plugin@"6.4.1" from the root project
npm ERR!
npm ERR! Fix the upstream dependency conflict, or retry
npm ERR! this command with --force or --legacy-peer-deps
npm ERR! to accept an incorrect (and potentially broken) dependency resolution.
npm ERR!
npm ERR!
npm ERR! For a full report see:
npm ERR! /Users/deku/.npm/_logs/2023-08-23T07_12_06_464Z-eresolve-report.txt

npm ERR! A complete log of this run can be found in: /Users/deku/.npm/_logs/2023-08-23T07_12_06_464Z-debug-0.log

The Conflicting peer dependency error occurs when the dependency of the package, you want to install, conflicts with the dependency of the already installed package. In other words, the Conflicting peer dependency error occurs when the version of the package required by the package, you want to install, conflicts with the version of the already installed package.

Let’s see the details of the error introduced in this blog post. In this blog post, the @typescript-eslint/parser package’s version 6.4.1 is already installed, but the @typescript-eslint/eslint-plugin package requires the version ^6.0.0 || ^6.0.0-alpha of the @typescript-eslint/parser package, so the installation cannot be performed and the Conflicting peer dependency error is displayed.

Solutions

The Conflicting peer dependency error can be solved in the following ways.

–force or –legacy-peer-deps option

To solve this problem, you can use the --force or --legacy-peer-deps option when running npm install as follows.

npm install [Package name] --force
npm install [Package name] --legacy-peer-deps

.npmrc file

The Conflicting peer dependency error does not occur frequently, but if the conflict problem occurs frequently in the current project, you can create a .npmrc file in the project folder and modify it as follows.

legacy-peer-deps=true

If you create a .npmrc file like this, it will behave the same as using the --legacy-peer-deps option every time you run the npm install command.

npm config

You can create a .npmrc file and set legacy-peer-deps directly, but you can also set the .npmrc file using the following command.

npm config --location=project set legacy-peer-deps=true

You can set the location option to global, user, or project, and if you set the location option to project, the .npmrc file will be created in the folder where the command was executed, and legacy-peer-deps=true will be automatically set.

Completed

Done! we’ve seen how to fix the Conflicting peer dependency error that often occurs when installing a package using NPM. If you have the Conflicting peer dependency error, please try the above method to fix it.

Was my blog helpful? Please leave a comment at the bottom. it will be a great help to me!

App promotion

You can use the applications that are created by this blog writer Deku.
Deku created the applications with Flutter.

If you have interested, please try to download them for free.

Posts