How to cache global npm packages

I have packages that need to be installed globally in order to lint my codebase before deploy. The command is:

npm install -g eslint@1.9.0 eslint-config-airbnb@1.0.0 eslint-plugin-react@3.8.0 babel-eslint@4.1.5

The log trace gives feedback like:

/home/ubuntu/nvm/versions/node/v4.2.1/bin/eslint -> /home/ubuntu/nvm/versions/node/v4.2.1/lib/node_modules/eslint/bin/eslint.js
eslint-config-airbnb@1.0.0 /home/ubuntu/nvm/versions/node/v4.2.1/lib/node_modules/eslint-config-airbnb

My question is: how can I cache these packages so I don’t need to keep waiting for them to install?

You can cache global modules by adding the following to your circle.yml:

dependencies;
  cache_directories:
    - /home/ubuntu/nvm/versions/node/<version>/bin
    - /home/ubuntu/nvm/versions/node/<version>/lib/node_modules

Alternatively, you can just use the local (automatically cached) version if it’s in your package.json as a dev dependency like so:

test:
  pre:
    - node node_modules/eslint/bin/eslint.js

Hope this helps!

4 Likes

@frank, I’m doing something similar in my build, but with coffeelint, and I’m having trouble actually running coffeelint after setting up my circle.yml.

How do I invoke the module after setting up circle.yml with the following:

test:
  pre:
    - node node_modules/coffeelint/lib/coffeelint.js

In the case of coffeelint, I believe you can invoke it with the following circle.yml:

test:
  pre:
    - ./node_modules/.bin/coffeelint
    - ./node_modules/coffeelint/bin/coffeelint # this works too
    - node node_modules/coffeelint/lib/commandline.js # this works as well

So I think /home/ubuntu/nvm is not correct any more and it actually needs to be /opt/circleci/nodejs/

However, as I specify my node version using the “v6” shorthand, and the directory that gets created is currently 6.9.3, I have no way of referencing that directory name inside my circle.yml file.

It would be great if I could put something in my circle.yml file that uses the installed directory name for node so that I can cache global npm packages.