How to solve the npm installation dependency error ERESOLVE unable to resolve dependency tree

Dependency management is already an indispensable part of modern front-end project development, and due to various problems, such as historical reasons, lack of project maintenance, etc., front-end projects will encounter many problems in dependency management. This article discusses one of them npm install, ERESOLVE unable to resolve dependency treethe reasons for the error and how to solve it.

error message

When installing dependencies in a project [email protected]that ali-react-table, the following error occurs. Carefully read the cause of the error and you can know that ali-react-tablethe use in peerDependenciesdefines a dependent react@"^16.8.0 || ^17.0.1"project, which conflicts with Reactthe version . Although this is because ali-react-tableit has neglected maintenance and has not updated the dependency version information, but our control over third-party dependencies is relatively low. In addition to waiting for third-party dependency updates or PRmentioning and waiting for merged releases, we have some other methods This problem can be solved temporarily.

npm ERR! code ERESOLVE
npm ERR! ERESOLVE unable to resolve dependency tree
npm ERR! 
npm ERR! While resolving: [email protected]
npm ERR! Found: [email protected]
npm ERR! node_modules/react
npm ERR! react@"^18.2.0" from the root project
npm ERR! 
npm ERR! Could not resolve dependency:
npm ERR! peer react@"^16.8.0 || ^17.0.1" from [email protected]
npm ERR! node_modules/ali-react-table
npm ERR! ali-react-table@"*" 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. 

Option 1: Downgrade

Dependency rule verification was introduced npm@7after , we can downgrade orNode.js bypass the verification without reporting an error.npm

nvm use 14.17.4

## or

npm i -g npm@6 

Option 2: -f or --legacy-peer-deps

In fact, we know ali-react-tablethat due to neglect of maintenance, we did not update the dependent version information in time. The actual test and [email protected]the one can run without any problem, then we can bring --forcethe parameter (abbreviation -f) when installing to tell npmto force the installation.

npm install -f 

Another parameter is that you can make the behavior of the old version without--legacy-peer-deps downgrading , refer to the documentation . However, the actual effect of this parameter may vary depending on the project, so you need to test it yourself.npmnpm install

npm install --legacy-peer-deps 

Option 3: yarnthe resolutionsor npmtheoverrides

In the actual project, there may not only be more than one problem that ali-react-tablethe dependency version is inconsistent with the dependency version required by the project. There may be many dependencies that have this problem. Sometimes we know the dependency version relationship of the project and can be used resolutions(only use yarncan be used , refer to the documentation ) or overrides(only npm@8the above can be used, refer to the documentation ) to specify and override the dependency version specified by the third-party package. This parameter is also very effective in other scenarios, such as the lack of maintenance of the required third-party dependencies, the specified version is a problematic version, and so on.

{"name": "project","version": "1.0.0","dependencies": {},"resolutions": {"react": "^18.2.0"}
} 
{"overrides": {"react": "^18.2.0"}
} 

Summarize

Dependency management is now an important part of front-end development. In addition to paying attention to third-party dependency version updates and whether major version updates are compatible Breaking Changewith projects, you must also choose appropriate third-party dependencies for your own projects and update dependent versions in a timely manner. Avoid dependency version problems affecting project development and project operation. When you encounter an error, you must read the error description to find out the root cause of the error, and prescribe the right medicine to find a suitable solution.

at last

Recently, I also sorted out a JavaScript and ES note, a total of 25 important knowledge points, and explained and analyzed each knowledge point. It can help you quickly master the relevant knowledge of JavaScript and ES, and improve work efficiency.



Friends in need, you can click the card below to receive and share for free

Guess you like

Origin blog.csdn.net/Android062005/article/details/129023738