如何防止在 node_modules 中嵌套 node_modules [英] How to prevent nested node_modules inside node_modules

查看:241
本文介绍了如何防止在 node_modules 中嵌套 node_modules的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了自己的 npm 包,我们称之为 XYZ,它在 package.json 文件中有 @material-ui 依赖项.

I've created my own npm package, let's call it XYZ, it has @material-ui dependency in it's package.json file.

当我在项目 AI 中安装它时,在 XYZ 文件夹中嵌套了 node_modules(所以它是 A\node_modules\XYZ\node_modules\@material-ui),但是当我在项目 BI 中安装它时'没有嵌套的 node_modules 文件夹.项目 A 和 B 的 package.json 文件中都有相同版本的 @material-ui.

When I install it in project A I have nested node_modules inside of XYZ folder(so it's A\node_modules\XYZ\node_modules\@material-ui), but when I install it in project B I don't have nested node_modules folder. Both project A and B has @material-ui in their package.json files with same versions.

如何强制我的 XYZ 包使用 A\node_modules 中的 @material-ui?

How to force my XYZ package to use @material-ui from A\node_modules?

推荐答案

优点是嵌套文件夹较少,缺点是直接在 node_modules 文件夹中有更多文件夹以及版本控制问题.

There are upside of having less nested folders and downside having more folders in node_modules folder directly and version control problems.

正确的yarnnpm(即:npm v3)不应该有这样的结构问题.它应该在可能的情况下始终展平整个结构,并且只有在版本与顶部的版本不兼容时才嵌套 node_modules.

Correct yarn and npm (ie: npm v3) should not have such structure issue. It should always flatten the whole structure where possible and only have nested node_modules if the versions are incompatible with the one at top.

因此,如果它在一个项目上正常运行,而在另一个项目上运行正常,则可能是由于版本原因.检查 @material-ui 是否在两者上的版本相同.也许两个不​​同的包在某个时候相互冲突.

So if you have it working properly on one project and not on another, its probably due to version. Check out if the @material-ui is same version on both. Maybe two different packages are conflicting with each other at some point.

从你的问题来看,它说它是相同的版本.但是,您没有提到您是如何在这两个项目上安装软件包的.如果您使用 yarn linknpm link 安装,它应该按预期正确安装依赖项.

From your question, it says it's same version. However, you did not mention how you installed your package on both project. If you install with yarn link or npm link it should install dependencies properly as expected.

如果你查看包,最近material-ui已被弃用,通知说要升级到@material-ui/core.可能是该文件夹中的某些包不相同.无论哪种方式,只要有一些依赖冲突,就会像这样.检查 @material-ui 文件夹内.

If you check the package, recently material-ui has been deprecated, and the notice says to upgrade to @material-ui/core instead. It might be some packages inside that folder is not same. Either way, it's like this whenever there is some dependency conflict. Check inside the @material-ui folder.

有几个软件包可以强制解决此问题.他们将通过嵌套的 node_modules 文件夹并将它们展平为单个文件夹.

There are several packages to forcefully resolve this issue. They will go thru the nested node_modules folders and flatten them into single folder.

  • 安装,npm install -g flatten-packages.
  • 运行可执行文件 flatten-packages 重新排列项目目录中 node_modules 文件夹中的所有包.
  • Flatten 将删除旧版本的包.您应该注意与版本中断更改相关的错误.
  • Install with, npm install -g flatten-packages.
  • Run executable flatten-packages to rearrange all packages in node_modules folder in the project directory.
  • Flatten will delete older version of a package. You should take care of version breaking changes related errors.

这篇关于如何防止在 node_modules 中嵌套 node_modules的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆