React JS npm start shows failed to compile web-vitals
Asked Answered
M

7

37

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;
Mancy answered 21/12, 2020 at 16:36 Comment(2)
Can't resolve 'web-vitals' : Seems like you should install web-vitals using npm i web-vitals.Convolution
Resolved after deleting & re-installing all packages. May be there was missing packages due to slow internet. Thanks Ajeet Shah.Mancy
C
83

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.

Corrinnecorrival answered 13/2, 2021 at 11:20 Comment(2)
For those using yarn: yarn add -D web-vitalsGisarme
Interesting, create-react-app gives an issue, but installing web-vital again sorts the problem.Vegetate
M
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

Mina answered 5/11, 2021 at 13:55 Comment(0)
T
3

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

Teriann answered 21/10, 2022 at 16:12 Comment(0)
G
1

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

Gahl answered 4/1, 2021 at 22:14 Comment(0)
S
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
Sassoon answered 13/1, 2022 at 13:7 Comment(1)
I did it and it did not work, you have to use npm install web-vitals.Vegetate
A
1

I have been struggling with this for a whole day, things I have tried are

  1. Removing modules/ package.lock.json
  2. Directly installing Web-Vitals.
  3. Updating node and NPM versions.

None of it worked, but I found this updated version of WebVital file and it worked. Please see if replacing the following with content of reportWebVitals file works for you.

You need to use (metric: MetricType) => void as Replacement type.

import { MetricType } from "web-vitals";

const reportWebVitals = (onPerfEntry?: (metric: MetricType) => void) => {
  if (onPerfEntry && onPerfEntry instanceof Function) {
    import("web-vitals").then(({ onCLS, onINP, onFCP, onLCP, onTTFB }) => {
      onCLS(onPerfEntry);
      onINP(onPerfEntry);
      onFCP(onPerfEntry);
      onLCP(onPerfEntry);
      onTTFB(onPerfEntry);
    });
  }
};

export default reportWebVitals;
Agency answered 11/6 at 17:30 Comment(0)
D
-2

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

Diagnostic answered 22/12, 2021 at 12:55 Comment(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.Colosseum
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.Vegetate

© 2022 - 2024 — McMap. All rights reserved.