Menu

Using Yarn (the npm replacement) on CircleCI

Yarn Logo

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 or machine executors, you can install Yarn by following the official instructions from Yarn Docs.

Caching

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

#...
      - restore_cache:
          name: Restore Yarn Package Cache
          keys:
            - yarn-packages-{{ .Branch }}-{{ checksum "yarn.lock" }}
            - yarn-packages-{{ .Branch }}
            - yarn-packages-master
            - yarn-packages-
      - run:
          name: Install Dependencies
          command: yarn install
      - save_cache:
          name: Save Yarn Package Cache
          key: yarn-packages-{{ .Branch }}-{{ checksum "yarn.lock" }}
          paths:
            - node_modules/
#...