# How to cache global npm packages

**URL:** <https://discuss.circleci.com/t/how-to-cache-global-npm-packages/951>\
**Category:** Build Environment\
**Tags:** nodejs, circle-yml\
**Created:** [December 5, 2015, 5:12pm UTC](https://discuss.circleci.com/t/how-to-cache-global-npm-packages/951 "2015-12-05T17:12:43Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![xijhing](https://sea2.discourse-cdn.com/circleci/user_avatar/discuss.circleci.com/xijhing/32/618_2.png) [@xijhing](https://discuss.circleci.com/u/xijhing)\
**Post date:** [December 5, 2015, 5:12pm UTC](https://discuss.circleci.com/t/how-to-cache-global-npm-packages/951/1 "2015-12-05T17:12:43Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![frank](https://sea2.discourse-cdn.com/circleci/user_avatar/discuss.circleci.com/frank/32/124_2.png) [@frank](https://discuss.circleci.com/u/frank)\
**Post date:** [December 5, 2015, 7:57pm UTC](https://discuss.circleci.com/t/how-to-cache-global-npm-packages/951/2 "2015-12-05T19:57:42Z")

</div>

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

```auto
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:

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

```

Hope this helps!

---

<div class="post-metadata">

**Author:** ![lsanwick](https://sea2.discourse-cdn.com/circleci/user_avatar/discuss.circleci.com/lsanwick/32/1357_2.png) [@lsanwick](https://discuss.circleci.com/u/lsanwick)\
**Post date:** [February 3, 2016, 12:01am UTC](https://discuss.circleci.com/t/how-to-cache-global-npm-packages/951/3 "2016-02-03T00:01:11Z")

</div>

@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:

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

```

---

<div class="post-metadata">

**Author:** ![frank](https://sea2.discourse-cdn.com/circleci/user_avatar/discuss.circleci.com/frank/32/124_2.png) [@frank](https://discuss.circleci.com/u/frank)\
**Post date:** [February 3, 2016, 12:54am UTC](https://discuss.circleci.com/t/how-to-cache-global-npm-packages/951/4 "2016-02-03T00:54:33Z")

</div>

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

```auto
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

```

---

<div class="post-metadata">

**Author:** ![dave-irvine](https://avatars.discourse-cdn.com/v4/letter/d/50afbb/32.png) [@dave-irvine](https://discuss.circleci.com/u/dave-irvine)\
**Post date:** [January 5, 2017, 2:30pm UTC](https://discuss.circleci.com/t/how-to-cache-global-npm-packages/951/5 "2017-01-05T14:30:40Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/circleci/original/3X/5/1/511608acda8e85906e15909b57bd0a46c1dbe291.png) [@system](https://discuss.circleci.com/u/system)\
**Post date:** [June 18, 2018, 11:37am UTC](https://discuss.circleci.com/t/how-to-cache-global-npm-packages/951/6 "2018-06-18T11:37:43Z")

</div>


