首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >解决方法:./src/reportWebVitals.js语法错误:'import‘和'export’可能只出现在顶层

解决方法:./src/reportWebVitals.js语法错误:'import‘和'export’可能只出现在顶层
EN

Stack Overflow用户
提问于 2021-10-16 17:26:22
回答 1查看 296关注 0票数 0

在没有备份包的情况下,我愚蠢地使用了命令npm audit fix --force,现在我在react应用程序中发现了一些错误。

每当我启动应用程序时,我都会收到错误消息:

/src/reportWebVitals.js语法错误:'import‘和'export’只能出现在顶层

我该如何解决这个问题?

我的reportWebVitals.js文件如下所示:

代码语言:javascript
复制
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;

我的package.json看起来像这样:

代码语言:javascript
复制
{
  "name": "duran_supplements_and_workout_equipment",
  "version": "0.1.0",
  "private": true,
  "proxy": "http://localhost:8000",
  "dependencies": {
    "@testing-library/jest-dom": "^5.11.4",
    "@testing-library/react": "^11.1.0",
    "@testing-library/user-event": "^12.1.10",
    "http-proxy-middleware": "^2.0.1",
    "mongoose": "^5.12.13",
    "react": "^17.0.2",
    "react-dom": "^17.0.2",
    "react-scripts": "^0.9.5",
    "socket.io-client": "^4.1.2",
    "web-vitals": "^1.0.1"
  },
  "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"
    ]
  },
  "description": "This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app).",
  "main": "index.js",
  "author": "",
  "license": "ISC"
}
EN

回答 1

Stack Overflow用户

发布于 2021-10-17 09:15:01

显然,我的反应脚本似乎已经被更新或降级,这导致了这个问题。我用过

代码语言:javascript
复制
npm uninstall react-scripts 

之后,

代码语言:javascript
复制
npm install react-scripts

我的问题就解决了。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69597981

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档