NAV

Using Yarn (the npm replacement) on CircleCI

Yarn Logo

Yarn is an open-source package manager for JavaScript. Yarn is pre-installed on CircleCI and the packages it installs can be cached. This can potentially speed up builds but, more importantly, can reduce errors related to network connectivity.

Version Support

Ubuntu 14.04:0.18.1

Ubuntu 12.04:n/a

macOS:n/a

Setup

When CircleCI detects a JavaScript project, certain commands (like npm install or npm test) might be run. To use Yarn instead of npm, we override both the dependencies and test sections which prevents npm and other inferred commands from running.

dependencies:
  override:
    - yarn
test:
  override:
    - yarn test

Overriding the dependencies phase means that the node_modules/.bin directory doesn’t get added to PATH. We add it in the machine phase.

machine:
  environment:
    PATH: "${PATH}:${HOME}/${CIRCLE_PROJECT_REPONAME}/node_modules/.bin"

Cache

Yarn stores packages for caching in ~/.cache/yarn. This is specified as a cache directory to CircleCI and will be cached after the dependencies phase completes.

dependencies:
  cache_directories:
    - ~/.cache/yarn

Full Example

machine:
  environment:
    PATH: "${PATH}:${HOME}/${CIRCLE_PROJECT_REPONAME}/node_modules/.bin"

dependencies:
  override:
    - yarn
  cache_directories:
    - ~/.cache/yarn

test:
  override:
    - yarn test