Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I have Travis cache node_modules or $HOME/.npm

I'm quite confused about which directory is optimal for caching. I've seen both used and recommended, but no actual comparison as to why go one way or the other.

For instance, the Travis blog itself recommends:

cache:   directories:     - node_modules 

However, thousands of places use this instead:

cache:   directories:     - $HOME/.npm 

So why use one over the other, and why not include both?

like image 959
balupton Avatar asked Mar 01 '17 01:03

balupton


People also ask

Should node_modules be cached?

As I understand the documentation about caching both in this repo and in the upstream actions/cache repo, the reason node_modules is not recommended to cache is because dependencies can break across versions of Node.

Is npm cache important?

The npm cache is strictly a cache: it should not be relied upon as a persistent and reliable data store for package data. npm makes no guarantee that a previously-cached piece of data will be available later, and will automatically delete corrupted contents.

Should I run npm cache clean?

If you ever get weird errors in npm like Please run npm cache clean you may need to clean or refresh your npm cache. To fix this, you can try running npm cache clean .

Can I delete npm cache folder?

Yes it is safe, I have deleted npm and npm-cache folder manually and reinstall node its working fine.


2 Answers

I noticed caching the node_modules folder caused problems (build fails) while caching the .npm cache avoided it. I believe it's because the .npm cache doesn't store compiled native modules while the node_modules folder does. So when you test different versions of node, as is common in Travis-CI, it will try to load a native module compiled for say node 4 in node 6 and barf.

like image 136
John-David Dalton Avatar answered Oct 10 '22 02:10

John-David Dalton


Follow up on @John's answer.

To strictly adhere to package dependencies on package-lock.json, the NPM install process on Travis CI now defaults to the new npm ci (ci stands for continuous integration, I think) instead of npm install. This helps prevent installing packages that are not following proper semantic versioning.

To do this, npm ci needs to first get rid of the dependency graph and all the cached compiled modules in node_modules from previous builds so to restructure the dependency graph. It does so by removing node_modules entirely before it begins its own installs. But that also means node_modules can no longer be used as cache location on Travis. We must now use "$HOME/.npm" to cache, and @John has explained the reason using "$HOME/.npm". Travis will throw an error at you complaining "/node_modules/.bin/npm cannot be found" if you continue to use node_modules as cache location, since node_modules has been deleted when running npm ci.

Now regarding which cache location to use...

  1. $HOME/.npm

    When you want to use the now default npm ci, include these changes in your .travis.yml

    # [optional] `npm ci` is now default on Travis install: - npm ci  # Keep the npm cache around to speed up installs cache:   directories:   - "$HOME/.npm" 
  2. node_modules

    If you wish to stick to the old npm install

    # Specify `npm install` install: - npm install  # Continue to use the old cache location cache:   directories:   - "node_modules" 

Warning: your cache location depends on the install method of your choice, and cannot be intertwined with another. Otherwise you risk losing the benefits of caching, or worse, have a failed Travis build.

You can find more about npm ci in the NPM docs.

like image 22
Hank Chan Avatar answered Oct 10 '22 02:10

Hank Chan