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

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.


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.