node.jsreactjsnpmnpm-installnext-auth

conflict next-auth with react 18


i get this error when installing next-auth with react 18

npm ERR! code ERESOLVE
npm ERR! ERESOLVE unable to resolve dependency tree
npm ERR! 
npm ERR! While resolving: gito-auth@0.1.0
npm ERR! Found: react@18.1.0
npm ERR! node_modules/react
npm ERR!   react@"18.1.0" from the root project
npm ERR! 
npm ERR! Could not resolve dependency:
npm ERR! peer react@"^16.13.1 || ^17" from next-auth@3.29.3
npm ERR! node_modules/next-auth
npm ERR!   next-auth@"*" 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! See C:\Users\mizzo\AppData\Local\npm-cache\eresolve-report.txt for a full report.

npm ERR! A complete log of this run can be found in:
npm ERR!     C:\Users\mizzo\AppData\Local\npm-cache\_logs\2022-04-27T14_07_48_165Z-debug-0.log

is theres any solution to fix it without using --force flag ?


Solution

  • Here's how I solved this problem:

    First, what's happening: next-auth is looking for react@"^16.13.1 || ^17", but it is not finding it. Instead it is finding react@18.1.0, which is a newer version. For some reason doesn't like this newer version, and you are being notified (it is not a big deal, but they decided it was worth stopping your build).

    One solution: forcibly install the specific version of react that next-auth wants:

    npm install react@18.1.0 What this does is roll back your react version to a slightly older one that is compatible with next-auth. You won't notice any difference, and in future iterations, hopefully next-auth is updated, so this goes away.

    Another solution: make a sweeping decision to not install any older version dependencies:

    npm install xxxx --legacy-peer-deps What this does is ignore old dependencies for this package. It is more comprehensive, and makes a lot of the decisions for you.