首页 > 解决方案 > Package-lock.json 文件未在分支中正确安装 npm

问题描述

[更新]:似乎可以通过package-lock.json从工作分支复制并将其粘贴到非工作分支来临时解决该问题。

这对我来说没有意义,因为不package-lock.json应该影响npm install.

尝试 npm install 时遇到一个奇怪的问题。首先,我从我的主分支开始并运行一个npm install,我的应用程序可以正常工作。然后我切换到另一个分支并git pull从我的主分支。然后我尝试运行npm install,之后当我尝试运行我的应用程序时,我收到以下错误:

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.41.2"

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:

  /Users/t.dammon/work/cpax/cpax-react/node_modules/webpack (version: 4.41.5) 

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

If you would 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 /Users/t.dammon/work/cpax/cpax-react/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 install。我还尝试了错误消息中列出的所有故障排除步骤。

我还比较了非工作分支和工作分支(两者都刚刚运行 npm install)中的包锁定文件,看起来非工作分支正在更新一些高于工作分支的依赖版本。

我发现真正有趣的是,如果我npm start在我的工作分支中运行并拉起我的应用程序然后切换分支,我可以停止并在非工作分支中启动我的应用程序(只要我不运行npm install)。

所以我的结论是npm install我的分支中正在发生一些时髦的事情,而其他地方没有发生,但我真的无法弄清楚究竟是什么导致了这个问题。

另一个额外的细节是,当我npm install在工作分支中运行时,我将有一个漏洞需要审计,而npm install在非工作分支中则不会发现漏洞。

标签: npmwebpackpackage-lock.json

解决方案


我也遇到过这个确切的错误,并且花了一段时间才弄清楚出了什么问题 - 提供的错误说明不起作用,运行npm outdated并将所有依赖项更新到最新版本都不起作用。即使将 package.json 文件的内容换成一个全新的 vianpx creat-react-app也不适合我。

解决方案:我检查了我的项目所在的文件夹,发现它也有一个node_modules文件夹。删除此文件夹为我清除了错误。这有点奇怪,因为删除的文件夹在我的项目文件夹之外,但看起来 NPM 正在目录树中向上工作,寻找任何node_modules文件夹。


推荐阅读