React 脚本启动时出现意外的令牌错误

React scripts start is giving Unexpected token error

我一直在使用 create-react-app 来 bootstrap 响应应用程序。 但我今天面临一个非常奇怪的问题。 在 bootstrap 使用 create-react-app ping 我的应用程序之后。 运行 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.

我已经试过了 - 删除 package.lock.json 并删除节点模块和 运行ning npm install 后跟 npm 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"
    ]
  }
}

非常感谢任何帮助。

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

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

  1. 删除了整个项目。由于到目前为止我还没有编写太多代码,所以我只是这样做了。如果您已经写了很多代码,请备份。
  2. 我安装了 NVM(Node Version Manager)。我用这个 link How to install NVM in Ubuntu 来安装它。
  3. 安装 nvm 后使用 nvm install 12.0.0(or the node version > 8.0.0 which you would like to use)。这实际上是我的问题。 create-react-app 到 bootstrap 成功你应该使用 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 和 bootstrap 应用程序。

这对我来说就像一个魅力。希望遇到同样问题的人会觉得它有用。

只需使用以下命令即可解决问题:

  • 12.0.0需要先安装才能使用

    • 第 1 步:首先 nvm install 12.0.0
    • 第 2 步:nvm use 12.0.0

注意:如果节点版本 12.0.0 已经存在,请跳过步骤 1

当部署到 Heroku 时,我遇到了同样的错误,在寻找比接受的解决方案更多的解决方案后,我发现 显然更容易。

这似乎是由于扩展运算符,从 Node 的 8.6 版开始可用。它建议在 package.json ("node" : ">=8.6").

的引擎部分中指定更高版本和更高版本

使用该解决方案后,它抱怨:“engines.node 中的危险语义版本范围 (>)”,指向 https://devcenter.heroku.com/articles/nodejs-support#specifying-a-node-js-version。 此 Heroku 的支持站点建议使用与本地相同的版本 运行,但建议在补丁中使用 x 以获取最新的补丁更新。最后我得到了 "node" : "12.x"。这对我来说很好。

我遇到了类似的问题。我通过在项目根目录上创建一个 .env 文件并将此 SKIP_PREFLIGHT_CHECK=true 粘贴到 .env 文件中来解决它。