【问题标题】:Create React App Dependency Issue with Webpack使用 Webpack 创建 React 应用程序依赖问题
【发布时间】:2018-10-09 09:44:27
【问题描述】:

我正在学习 Lynda 的 Learning React 课程。在第三章的第二个视频中,我遇到了一个问题。当我在 Git Bash 上输入 npm start 时,显示以下错误:

$ npm start

> bulletin-board@0.1.0 start C:\Users\John\Desktop\Ex_Files_Learning_Reactjs\Files\Ch03\03_02\start\bulletin-board
> react-scripts start


There might be a problem with the project dependency tree.
It is likely not a bug in Create React App, but something you need to fix locally.

The react-scripts package provided by Create React App requires a dependency:

  "webpack": "4.19.1"

Don't try to install it manually: your package manager does it automatically.
However, a different version of webpack was detected higher up in the tree:

  C:\Users\John\node_modules\webpack (version: 2.5.1)

Manually installing incompatible versions is known to cause hard-to-debug issues.

If prefer to ignore this check, add SKIP_PREFLIGHT_CHECK=true to an .env file in your project.
That will permanently disable this message but you might encounter other issues.

To fix the dependency tree, try following the steps below in the exact order:

  1. Delete package-lock.json (not package.json!) and/or yarn.lock in your project folder.
  2. Delete node_modules in your project folder.
  3. Remove "webpack" from dependencies and/or devDependencies in the package.json file in your project folder.
  4. Run npm install or yarn, depending on the package manager you use.

In most cases, this should be enough to fix the problem.
If this has not helped, there are a few other things you can try:

  5. If you used npm, install yarn (http://yarnpkg.com/) and repeat the above steps with it instead.
     This may help because npm has known issues with package hoisting which may get resolved in future versions.

  6. Check if C:\Users\John\node_modules\webpack is outside your project directory.
     For example, you might have accidentally installed something in your home folder.

  7. Try running npm ls webpack in your project folder.
     This will tell you which other package (apart from the expected react-scripts) installed webpack.

If nothing else helps, add SKIP_PREFLIGHT_CHECK=true to an .env file in your project.
That would permanently disable this preflight check in case you want to proceed anyway.

P.S. We know this message is long but please read the steps above :-) We hope you find them helpful!

npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! bulletin-board@0.1.0 start: `react-scripts start`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the bulletin-board@0.1.0 start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     C:\Users\John\AppData\Roaming\npm-cache\_logs\2018-10-09T09_26_01_403Z-debug.log

我遵循了第 1 步和第 2 步。但是我在第 3 步遇到了一个问题,我的项目文件夹中的 package.json 文件中没有来自依赖项和/或 devDependencies 的“webpack”。

所以,我按照步骤 4 运行 npm start。但我得到了同样巨大的错误线。我应该怎么做才能解决这个问题?

我的 npm 版本是 5.6.0。

我被卡住了,如果不修复它就无法继续学习。 NPM 让我非常恼火。我正在上 Angular 的课程,并面临 NPM 的很多问题。现在,我正在学习 React 课程,但我又一次受到了很多问题的影响。真的很烦。

更新: 以下是 package.json 中的代码:

{
  "name": "bulletin-board",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "react": "^16.5.2",
    "react-dom": "^16.5.2",
    "react-scripts": "^2.0.4"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  },
  "eslintConfig": {
    "extends": "react-app"
  },
  "browserslist": [
    ">0.2%",
    "not dead",
    "not ie <= 11",
    "not op_mini all"
  ]
}

【问题讨论】:

  • 您能展示一下您的 package.json 文件中的内容吗?
  • 我已经用 package.json 文件中的代码更新了我的问题。请看一下。
  • 我在 Mac 上遇到了同样的问题。遵循输出中的所有说明,但没有运气。添加 SKIP_PREFLIGHT_CHECK=true 似乎是个 hack。
  • 你学的是同一门课吗?你想一起学习吗?

标签: reactjs npm


【解决方案1】:

是的,我遇到了同样的问题。
我刚刚在C:\Users\username 的主目录中删除了node_modules。
然后,运行npm start,它成功了。

【讨论】:

  • 也适用于 mac
  • 也适用于 Windows
【解决方案2】:

不需要卸载node.js,查看C:\Users\John 下是否有node_modules 文件夹并删除。

【讨论】:

    【解决方案3】:

    好的。我已经设法解决了这个问题。我已按照以下步骤操作:

    1. 卸载 node.js。
    2. 从以下位置删除 node_modules 文件夹:C:\Users\John\node_modules
    3. 重新安装 node.js。

    现在,当我在 Git Bash 的项目文件夹中键入 npm start 时,一切正常!

    【讨论】:

      【解决方案4】:

      当您遇到此问题时,您可以使用以下命令返回到支持的 eslint 版本,而不是执行所有推荐的步骤:npm install eslint@^(version)。

      例如,如果支持的版本是6.6.0,则只需运行npm install eslint@^6.6.0,就可以了。

      【讨论】:

        【解决方案5】:

        只需再次安装 webpack 即可获得最新版本的 webpack。因为 Create React App 提供的 react-scripts 包需要依赖最新版本 "webpack": "x.x.x"

        使用 npm 安装:

        npm install --save-dev webpack
        

        用纱线安装:

        yarn add webpack --dev
        

        【讨论】:

          【解决方案6】:

          运行npm ls webpack 并查看哪些依赖项正在使用它或安装另一个版本。尝试更新这些库。

          就我而言,它是故事书。我更新了它(及其插件)并且它工作正常。它可能会破坏某些东西,但您应该去文档并更新您的代码。

          它对我有用。

          【讨论】:

            【解决方案7】:

            pnpm

            如果您使用的是 pnpm,您可以在 package.json 文件中添加覆盖规则:

              "pnpm": {
                "overrides": {
                  "webpack": "4.44.2"
                }
              },
            

            或者,您可以安装 webpack@4.44.2 作为直接依赖项。


            来源:https://github.com/pnpm/pnpm/issues/4076

            【讨论】:

              猜你喜欢
              • 2020-06-20
              • 1970-01-01
              • 2020-05-09
              • 2021-07-08
              • 1970-01-01
              • 2021-11-30
              • 1970-01-01
              • 2018-01-17
              • 1970-01-01
              相关资源
              最近更新 更多