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\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.
How to force my XYZ package to use @material-ui
from A\node_modules
?
If you're using npm : Run npm dedupe after installing packages to remove nested duplicates. You can try deleting your package-lock. json and do a fresh install.
While doing npm install, inside node_modules . staging folder is getting created. Reasons: This is a temporary folder where the modules will be kept untill npm downloads all the modules specified in the package.
They are no longer recommending the node_modules folder be committed. Usually, no. Allow npm to resolve dependencies for your packages.
npm ci will do the following things: It will delete your node_modules folder to ensure a clean state. It will look in your package-lock.
There are upside of having less nested folders and downside having more folders in node_modules folder directly and version control problems.
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.
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.
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.
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.
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
npm install -g flatten-packages
.flatten-packages
to rearrange all packages in node_modules folder in the project directory. You can use npm dedupe
command to accomplish this.
You can put the command in postinstall
script in package.json
, and every time NPM installs package, the npm dedupe
command will flatten all the duplicated packages in same version for you.
For more information, see https://docs.npmjs.com/cli/dedupe
npm postinstall script
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With