【问题标题】:Can't run npm install on brand new create-react-app无法在全新的 create-react-app 上运行 npm install
【发布时间】:2019-08-14 15:00:07
【问题描述】:

尝试使用 create-react-app 模块创建 React 应用程序时,我遇到了一个非常奇怪的错误。在我的电脑上很容易重现错误:

npx create-react-app test-project --typescript

该项目运行良好。但是,如果我删除 node_modules 文件夹并执行以下操作:

npm install

我得到这个错误:

 npm install
    npm WARN deprecated flatten@1.0.2: I wrote this module a very long time ago; you should use something else.
    npm WARN deprecated left-pad@1.3.0: use String.prototype.padStart()
    npm ERR! Unexpected end of JSON input while parsing near '...3s4pkvleO6z4pcCFRth5v'

    npm ERR! A complete log of this run can be found in:
    npm ERR!     /Users/computer/.npm/_logs/2019-08-14T14_44_36_086Z-debug.log

有趣的是,如果我从 package.json 文件中删除“react-scripts”,npm install 就会成功 - 但当然该项目无法运行。

我使用的是节点 12.8.0。这里是 package.json 文件:

{
  "name": "test-project",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "@types/jest": "24.0.17",
    "@types/node": "12.7.1",
    "@types/react": "16.9.1",
    "@types/react-dom": "16.8.5",
    "react": "^16.9.0",
    "react-dom": "^16.9.0",
    "react-scripts": "3.1.1",
    "typescript": "3.5.3"
  },
  "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 npm npm-install


    【解决方案1】:

    我通过运行解决了这个问题

    npm cache clean --force
    

    但是如果你想在对整个缓存进行核对之前检查它是否是一个可行的解决方案,你可以这样做:

    npm install --cache /tmp/empty-cache
    

    它强制 npm 不使用缓存。

    【讨论】:

      【解决方案2】:

      我克隆了你的 package.json 和 npm install 工作得很好 使用节点 v11.14.0 和 npm 6.7.0

      【讨论】:

        【解决方案3】:

        我试图解决这个问题,以便在节点 v12.8.1 上使用。

        但我无法解决。

        最后,我将节点版本降级为 10.16.3 LTS。 :(

        如果你想使用没有 npm 错误的 'create-react-app',只需使用节点版本 10.16.3 LTS。

        【讨论】:

          猜你喜欢
          • 2020-09-23
          • 2021-05-18
          • 2019-06-11
          • 1970-01-01
          • 2019-07-03
          • 2018-04-01
          • 2021-02-27
          • 2020-08-27
          • 2020-01-21
          相关资源
          最近更新 更多