34

I'm getting the error:

failed to compile -/src/reportWebVitals.js Module not found: Can't resolve 'web-vitals'. Since new to react JS, could not find what happened. Here is the reportWebVitals.JS file. Thanks in advance for the help. "/src/reportWebVitals.js Module not found: Can't resolve 'web-vitals' in 'E:\ReactResources\RectProjects\test-app\src'"

const reportWebVitals = onPerfEntry => {
if (onPerfEntry && onPerfEntry instanceof Function) {
import('web-vitals').then(({ getCLS, getFID, getFCP, getLCP, getTTFB }) => {
  getCLS(onPerfEntry);
  getFID(onPerfEntry);
  getFCP(onPerfEntry);
  getLCP(onPerfEntry);
  getTTFB(onPerfEntry);
});
}};
export default reportWebVitals;
Liam
  • 27,717
  • 28
  • 128
  • 190
ssajid.ru
  • 459
  • 1
  • 4
  • 9
  • 2
    _Can't resolve 'web-vitals'_ : Seems like you should install `web-vitals` using `npm i web-vitals`. – Ajeet Shah Dec 21 '20 at 17:33
  • 2
    Resolved after deleting & re-installing all packages. May be there was missing packages due to slow internet. Thanks Ajeet Shah. – ssajid.ru Dec 21 '20 at 18:19

6 Answers6

82

You need to install web vitals. Open your terminal and run the following command:

npm i web-vitals --save-dev

Also, deleting the whole thing and reinstalling might work, but that's a longer process.

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
Kelly Austin
  • 859
  • 4
  • 7
10

For anyone coming here from starting a brand new app with create-react-app: If you are using npm but run create-react-app without explicitly specifying it, it will use yarn when available. I was able to bypass this issue by adding the flag --use-npm.

So, for example:

npx create-react-app my-app --use-npm

Source

Alvaro
  • 1,853
  • 18
  • 24
3

for me following worked
first do npm uninstall react-scripts
then do npm install react-scripts

1

Delete all the files & Folders inside test-app folder. Reinstall all the packages now and start npm. It will work.

passionFinder
  • 121
  • 1
  • 13
1

If you have created your project with create-react-app, all the dependencies should be there when you run npm install.

Make sure you did that by navigating to the root directory and running:

npm install
Ikdemm
  • 1,804
  • 1
  • 11
  • 25
-2

I closed my terminal and opened again. Works for me!

  • 2
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Dec 22 '21 at 13:35
  • 1
    No, this is not correct, web vitals fails due to a wrong package check, which implies that closing the terminal will not sort the issue. – Hola Soy Edu Feliz Navidad Feb 14 '22 at 04:36