28

npx create-react-app my-project results in the following dependency errors:

npx version: 8.5.0

Installing template dependencies using npm...
npm ERR! code ERESOLVE
npm ERR! ERESOLVE unable to resolve dependency tree
npm ERR! 
npm ERR! While resolving: react-18@0.1.0
npm ERR! Found: react@18.0.0
npm ERR! node_modules/react
npm ERR!   react@"^18.0.0" from the root project
npm ERR! 
npm ERR! Could not resolve dependency:
npm ERR! peer react@"<18.0.0" from @testing-library/react@12.1.5
npm ERR! node_modules/@testing-library/react
npm ERR!   @testing-library/react@"^12.0.0" 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.

The command still produces a project directory, but running npm start in the created directory errors with web-vitals missing from node-modules.

Solutions tried

  • Running the same command with --force or --legacy-peer-deps as suggested by the above error message doesn't solve the problem.

  • Deleting node_modules and package-lock.json and running npm i also doesn't solve the problem.

Update

The problem has been fixed with the latest update of create-react-app. Now it creates a project without any problem.

hayata_suenaga
  • 642
  • 1
  • 5
  • 16

7 Answers7

15

Until this is fixed for now you can delete the node_modules folder and package-lock.json. Next, open package.json and change
"react": "^18.0.0" & "react-dom": "^18.0.0" to an earlier version e.g:
"react": "^17.0.2" & "react-dom": "^17.0.2".
Finally, you can run npm install.

Alternative Solution (Try this first!):
solution suggested by joooni1998):

  1. delete both node_modules and package-lock.json
  2. run npm i web-vitals --save-dev
  3. run npm install

and then you can use npm run build and npm start again

Gavriel
  • 190
  • 1
  • 5
  • 2
    Any idea on what's causing the underlying issue? Seems to be a webpack config. (I'm having the same issue). – DigiFreeze Apr 12 '22 at 02:17
  • 2
    I tried Gavriel's solution. The downloaded node-modules are missing web-vitals. Does someone have any idea why create-react-app can result in missing dependencies? – hayata_suenaga Apr 12 '22 at 02:29
  • 2
    @DigiFreeze I think the issue is being cause by a recent merge they did to the "react-testing-library". They changed it to only supports React < 18. You can read more [here](https://github.com/facebook/create-react-app/issues/12269) & [here](https://github.com/testing-library/react-testing-library/pull/1041) – Gavriel Apr 12 '22 at 03:26
  • 2
    it didn't worked for me – Vinoth Apr 12 '22 at 09:47
  • @hayata_suenaga try running `npm i web-vitals --save-dev` if it gives out the error : 'Module not found: Error: Can't resolve 'web-vitals'' – dreamers-blog Apr 12 '22 at 11:37
  • If it is case even after 6 month, we have to downgrade to ` "react": "^17.2.0", "react-dom": "^17.2.0", "react-scripts": "5.0.0"` – Jaden Oct 29 '22 at 03:27
7

Check here for the Github issue.

Here is a temporary workaround:

  • Install cra-template to a separate folder (other than your new project app's folder) using "npm install cra-template"

  • Go to the installed cra-template package folder and in file "template.json" change the line '"@testing-library/react": "^12.0.0"' to '"@testing-library/react": "^13.0.0"'

  • Go back to your root folder and run npx create-react-app my-app (your app name) --template file:PATH_TO_YOUR_CUSTOM_TEMPLATE

Additionally, you can let the build fail, remove the node_modules folder as well as the package-json.lock file. Open the package.json file and change the react and react-dom to an earlier version.

For example:

{
  "name": "frontend",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "cra-template": "1.1.3",
    "react": "^17.0.2",
    "react-dom": "^17.0.2",
    "react-scripts": "5.0.0"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  },
  "eslintConfig": {
    "extends": [
      "react-app",
      "react-app/jest"
    ]
  },
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  }
}

Delete reportWebVitals.js Remove import reportWebVitals from './reportWebVitals'; from index.js Remove reportWebVitals(); from the bottom of index.js

Finally run npm install

DougM
  • 920
  • 1
  • 9
  • 21
  • Thank you! Can confirm this works for the typescript template (the name of the package is cra-template-typescript). I didn't have to remove reportWebVitals either. – Akshit Baunthiyal Apr 12 '22 at 13:42
  • I wanted to take the custom templating approach but when I run "npm install cra-template" in a different directory I get the following message "up to date, audited 4 packages in 1s" but nothing is installed in that directory. There is no template.json to modify. Any pointers on this would be really appreciated. – Jonny Apr 12 '22 at 16:43
  • `react`, `react-dom`, `@testing-library/react` - which of the mentioned packages is the culprit? – Bergi Apr 12 '22 at 19:26
2

It worked for me by deleting both 'node_modules' folder and 'package-lock.json' file. Then, I changed both react and react-dom version to 17.0.2 instead of 18.0.0 inside the package.json file as suggested by Gavriel's answer https://stackoverflow.com/a/71836018/12490294. Then, inside the app folder, I ran

    npm i web-vitals --save-dev

Then

    npm start

The app started successfully

1

Or you can try this npx --legacy-peer-deps create-react-app my-appname or goto index.js and remove the usage of web vitals

or downgrade npm with npm install -g npm@8.3.0 and create react app.

0

You could use yarn add create-react-app your-app instead

Khanh D Trần
  • 164
  • 1
  • 3
0

Been over a year since this was asked, and in spite of the suggestion by OP that it's been fixed in new versions of create-react-app, it still hasn't been. Also, downgrading react in package.json didn't work. Had to take the risk and run the command with --legacy-peer-deps before I could get my app working.

Cedric Ipkiss
  • 5,662
  • 2
  • 43
  • 72
-2

Try running either with --force or with --legacy-peer-deps, both will install dependencies without problems.

If that doesn't work, then delete the node modules folder and start installing from scratch

Eragon_18
  • 720
  • 7
  • 11