React 脚本开始给出意外的令牌错误

A.K*_*.47 4 reactjs react-scripts

我一直在使用create-react-app引导反应应用程序。但我今天面临一个非常奇怪的问题。使用create-react-app. 我运行后面临以下问题npm start

rbac-tutorial-app/node_modules/@hapi/joi/lib/types/object/index.js:254
                        !pattern.schema._validate(key, state, { ...options, abortEarly:true }).errors) {
                                                                ^^^

SyntaxError: Unexpected token ...
    at createScript (vm.js:74:10)
    at Object.runInThisContext (vm.js:116:10)
    at Module._compile (module.js:533:28)
    at Object.Module._extensions..js (module.js:580:10)
    at Module.load (module.js:503:32)
    at tryModuleLoad (module.js:466:12)
    at Function.Module._load (module.js:458:3)
    at Module.require (module.js:513:17)
    at require (internal/module.js:11:18)
    at Object.<anonymous> (rbac-tutorial-app/node_modules/@hapi/joi/lib/types/func/index.js:5:20)
    at Module._compile (module.js:569:30)
    at Object.Module._extensions..js (module.js:580:10)
    at Module.load (module.js:503:32)
    at tryModuleLoad (module.js:466:12)
    at Function.Module._load (module.js:458:3)
    at Module.require (module.js:513:17)
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! rbac-tutorial-app@0.1.0 start: `react-scripts start`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the rbac-tutorial-app@0.1.0 start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
Run Code Online (Sandbox Code Playgroud)

我已经尝试过 - 删除 package.lock.json 并删除节点模块并运行npm installnpm start但问题仍然存在。- 更改我的节点和 npm 版本。

我正在使用的 NPM 版本 ->6.9.0 我正在使用的节点版本 ->v8.0.0

下面是我的package.json文件

{
  "name": "rbac-tutorial-app",
  "version": "0.1.0",
  "private": true,
  "devDependencies": {
    "babel-cli": "^6.26.0",
    "babel-core": "^6.26.0",
    "babel-plugin-transform-class-properties": "^6.24.1",
    "babel-plugin-transform-object-rest-spread": "^6.26.0",
    "babel-preset-env": "^1.6.1",
    "babel-preset-react": "^6.24.1"
  },
  "dependencies": {
    "react": "^16.8.6",
    "react-dom": "^16.8.6",
    "react-router": "^5.0.0",
    "react-router-dom": "^5.0.0",
    "react-scripts": "3.0.1"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  },
  "eslintConfig": {
    "extends": "react-app"
  },
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  }
}
Run Code Online (Sandbox Code Playgroud)

任何帮助将不胜感激。

A.K*_*.47 7

经过几个令人沮丧的几个小时寻找答案并尝试不同的方法后,我认为这不是我的代码的问题,而是create-react-app使用node version v8.0.0.

所以我删除了整个项目并按照以下步骤解决了我的问题 -

  1. 删除了整个项目。由于到目前为止我还没有编写太多代码,所以我只是这样做了。如果你已经写了很多代码,请备份。
  2. 我安装了NVM(Node Version Manager)。我使用这个链接如何在 Ubuntu 中安装 NVM来安装它。
  3. 安装 nvm 后,使用nvm install 12.0.0(or the node version > 8.0.0 which you would like to use). 这实际上是我的问题。为了create-react-app成功引导,您应该使用node version > 8.0.0.
  4. 在此之后我使用nvm use 12.0.0(or the node version which you have recently installed).
  5. 使用node -v. 它应该显示您在步骤 4 中要求使用的节点版本。
  6. 就是这样。现在您应该create-react-app再次使用并引导该应用程序。

这对我来说就像一种魅力。希望面临同样问题的人会发现它有帮助。

  • 同样的问题。但是,我使用的节点版本 &gt; 8.0.0(节点 8.2.1,完全是 npm 5.3.0)并且它不起作用。这是服务器端,在 CI 上。在我的 PC 上,我使用节点 8.11.1 和 npm 5.6.0,没问题... (2认同)