reactjs - 使用 Webpack 创建 React 应用程序依赖问题

标签 reactjs npm

我正在学习 Lynda 的 Learning React 类(class)。在第三章的第二个视频中,我遇到了一个问题。当我在 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 文件中的依赖项和/或 devDependency 中没有“webpack”。

因此,我按照步骤 4 运行 npm start。但我也遇到了同样的大量错误。我应该做什么来解决这个问题?

我的 npm 版本是 5.6.0。

我陷入困境,如果不修复它就无法继续学习类(class)。 NPM 让我非常恼火。我正在学习 Angular 类(class),并面临很多 NPM 问题。现在,我正在学习 React 类(class),我再次受到大量问题的影响。实在是太烦人了。

更新: 以下是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"
  ]
}

最佳答案

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

关于reactjs - 使用 Webpack 创建 React 应用程序依赖问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52717959/

相关文章:

javascript - ReactJS 使用 useState 双重渲染 bool 状态

reactjs - RelayJS 整数查询参数的不变违规

javascript - 根据用户输入 javascript 返回过滤后的数组

javascript - 在 React Native 中安排本地通知不起作用

node.js - 无法运行 nodejs 项目 - npm ERR!代码 EBADPLATFORM

node.js - 阻止 EBS Linux 2 (Node.js) 尝试执行 npm 安装?

javascript - 将语义 ui 导入到 React 组件中

javascript - 无法让 Plotly.js 图形在 react-grid-layout 中调整大小

node.js - npm install 无法在具有 git 依赖项的 Windows 上运行

javascript - 如何在nodejs中重复执行