Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

npm - How to prevent nested node_modules inside node_modules

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

When I install it in project A I have nested node_modules inside of XYZ folder(so it's A ode_modulesXYZ ode_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.

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

Use correct npm version

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.

Check versions

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.

Check how you are installing them

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.

Check if you are using different packages

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.

Flatten them manually (dangerous)

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

flatten-packages

  • 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.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...