Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.npm cache vs node_modules size

When building our node application, we are using .npm cache as follows:

npm install --cache /tmp/node-cache/.npm --prefer-offline

However I have reasons to believe, the cache is not working properly as:

/tmp/node-cache/ # du -sh .npm/
177M    .npm/

/tmp/build/ # du -sh node_modules/
1.1G    node_modules

Does this mean the build downloaded additional 900 M and not caching them in .npm directory?

like image 972
Vojtěch Avatar asked Oct 28 '25 08:10

Vojtěch


1 Answers

Does this mean the build downloaded additional 900 M and not caching them in .npm directory?

That's possible. But it's also possible that the difference is due to a combination of compression and build artifacts.

The npm cache is compressed. node_modules directories are uncompressed.

Furthermore, npm almost certainly only caches the source code. If you are using something like node-sass, PhantomJS, puppeteer, or some other thing that has a custom install script or other build step, then build artifacts may balloon your node_modules.

I would not necessarily conclude merely from the size difference that the cache is not being respected. You'll have to do more investigation. One thing you might try is:

  • Remove node_modules (and package-lock.json if it exists)
  • npm cache clean --force and confirm that the cache is very small
  • npm install
  • Check the sizes of node_modules and the cache again.
like image 142
Trott Avatar answered Oct 29 '25 22:10

Trott



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!