【问题标题】:React scripts start is giving Unexpected token errorReact 脚本启动给出了 Unexpected token 错误
【发布时间】:2019-05-13 10:56:06
【问题描述】:

我一直在使用create-react-app 来引导 React 应用程序。 但我今天面临一个非常奇怪的问题。 使用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.

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

任何帮助将不胜感激。

【问题讨论】:

    标签: reactjs react-scripts


    【解决方案1】:

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

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

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

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

    【讨论】:

    • 同样的问题。但是,我使用的节点版本 > 8.0.0(节点 8.2.1,npm 5.3.0 完全正确)并且它不起作用。这是服务器端,在 CI 上。在我的 PC 上,我使用 node 8.11.1 和 npm 5.6.0,没问题...
    • 在我的情况下,我在当天早些时候切换到 Node 6 版本并忘记了它:D
    【解决方案2】:

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

    • 需要安装12.0.0才能使用

      • 第一步:首先nvm install 12.0.0
      • 第二步:nvm use 12.0.0

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

    【讨论】:

      【解决方案3】:

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

      这似乎是由于 Node 的 8.6 版本中提供的扩展运算符。它建议在 package.json ("node" : ">=8.6") 的引擎部分指定更高版本和更高版本。

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

      【讨论】:

        【解决方案4】:

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-08-22
          • 1970-01-01
          • 2018-07-10
          • 2017-05-02
          • 2016-10-01
          • 2017-12-29
          相关资源
          最近更新 更多