Using Yarn (an NPM alternative) on CircleCI

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

Using Yarn in CircleCI

Yarn might already be installed in your build environment if you are using the docker executor. With Pre-built CircleCI Docker Images, the NodeJS image (circleci/node) already has Yarn preinstalled. If you are using one of the other language images such as circleci/python or circleci/ruby, there are two image variants that will include Yarn as well as NodeJS. These would be the -node and -node-browsers image variants. For example, using the Docker image circleci/python:3-node will give you a Python build environment with Yarn and NodeJS installed.

If you’re using your own Docker image base or the macos, windows or machine executors, you can install Yarn by following the official instructions from Yarn Docs. The Yarn Docs provide several installation methods depending on what machine executor you might be using. For example, you can install on any unix-like environment using the following curl command.

curl -o- -L | bash


Yarn packages can be cached to improve CI build times. Here’s an example:

      - restore_cache:
          name: Restore Yarn Package Cache
            - yarn-packages-{{ checksum "yarn.lock" }}
      - run:
          name: Install Dependencies
          command: yarn install --immutable
      - save_cache:
          name: Save Yarn Package Cache
          key: yarn-packages-{{ checksum "yarn.lock" }}
            - ~/.cache/yarn

See Also

Caching Dependencies