What are Yarn Offline Caches / Yarn Offline Mirror? Do You Need Them?

Learn how to set up and leverage offline caching in Yarn to streamline dependency management, improve build speeds, and ensure reliability in CI/CD pipelines. This guide covers offline cache benefits, use cases, setup instructions, and troubleshooting tips for seamless offline installations.

What are Yarn Offline Caches / Yarn Offline Mirror? Do You Need Them?

Offline caching in Yarn allows developers to download and store packages locally, enabling installations without a constant internet connection. Once a package is downloaded, it’s available for future installations from the local cache, bypassing the need for a remote registry. Offline caches are especially valuable in environments with limited or unreliable internet connectivity and help streamline development in secure environments with restricted internet access.

Use Cases for Offline Caches

Offline caches come in handy in a range of scenarios:

  • Continuous Integration (CI) Pipelines: By caching dependencies offline, builds become faster and more reliable, reducing the need to redownload packages during every CI build.
  • Offline or Limited Connectivity Environments: Developers working remotely, in areas with poor internet connectivity, or on the go can still manage dependencies without an internet connection.
  • Air-Gapped or Secure Environments: Some organizations have strict security policies and do not allow internet access. Offline caches enable dependency management without violating these policies.
  • Consistent Dependency Management: Offline caches ensure consistency across environments, as dependencies are sourced locally and are less susceptible to version mismatches.
  • Private Repositories: By caching these packages locally, you can bypass the need to re-authenticate and re-download them for every build, streamlining the process. This cache will allow the pipeline to quickly retrieve private dependencies from a local cache, reducing both build time and the need for repeated authentication.

Benefits of Using Offline Caches

  1. Speed: Installing packages from a local cache is faster than fetching them from a remote server, which can improve developer productivity and reduce CI build times.
  2. Reliability: Reduces dependence on network availability and remote registries, minimizing the impact of internet issues or server downtime.
  3. Version Stability: Cached dependencies are version-locked, reducing the risk of unexpected changes or conflicts due to package updates.

Now, let's talk about how we can set the yarn-offline-cache or yarn-offline-mirror

Setting up yarn-offline-cache

Yarn’s offline cache is a feature that allows you to store downloaded packages locally, making it possible to reinstall packages without accessing the internet. By enabling Yarn’s offline mode, dependencies are stored in a designated folder, and Yarn uses these cached files instead of fetching them from the network on future installs.

How to Set Up Yarn Offline Cache

Step 1: Configure the Offline Cache Directory
Start by setting up a specific directory for your offline cache, which Yarn will use to store packages.

yarn config set yarn-offline-mirror "./.yarn-offline-cache"

This command creates a .yarn-offline-cache folder in your project directory to store all downloaded packages. You can also configure this in a shared location for multiple projects by specifying a global path. The name can be anything for this directory.

This will create a file .yarnrc with some configurations like mapping the directory with the offline cache.

# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
# yarn lockfile v1


yarn-offline-mirror "./yarn-offline-cache"
yarn-offline-mirror-pruning true

You can also set up yarn-offline-mirror-prunning true This property helps clean the old cache and cleans it every time you install a new package or remove a new one.

Step 2: Install Dependencies
Once you’ve set the offline cache directory, install your dependencies as usual:

yarn install

Yarn will store all packages in the .yarn-offline-cache directory. The next time you install dependencies, Yarn will use the cached versions if they’re available, enabling offline installation.

Then, next time to install packages from the cache itself you can use

yarn install --offline

This command will install the dependencies from the cache. This will improve the CI build times and pipelines can pass very quickly.

Syncing Dependencies with Yarn Offline Cache

If there are any version mismatches or package updates, you may encounter situations where the offline cache doesn’t match the yarn.lock file. To address this, you can force Yarn to update dependencies and sync with the latest requirements.

  • Clear the Cache and Reinstall:
yarn cache clean
yarn install --offline

This will clear the existing cache and reinstall dependencies based on the yarn.lock file, ensuring that all dependencies are in sync.

  • Updating the Offline Cache: If you need to update dependencies in your cache (for example, after modifying package.json), run:
yarn install --update-checksums

This command checks for new versions and updates the offline cache as needed, ensuring consistency.

  • Sometimes, the dependencies required by a package may not install correctly in the cache, which can cause issues during builds or installations. To resolve this, you can remove the cache folder and then perform a fresh, forced installation of all dependencies:
yarn install --force

Offline cache in Yarn is a valuable tool for improving dependency management in both development and CI/CD environments. Whether you're working in a low-connectivity area, managing dependencies in a secure network, or simply looking to speed up builds, offline caching can be incredibly beneficial.

Setting up and syncing offline caches may require some initial effort, but the performance benefits and reliability they bring make them worthwhile, especially in environments with limited or unreliable internet access.

That's it for this article. See ya 👋


Some FAQS

What is Yarn’s offline cache, and how is it different from the standard cache?

  • Yarn’s offline cache allows you to store downloaded packages in a specified local directory, enabling installations without an internet connection. Unlike the standard cache, which Yarn manages automatically, the offline cache directory is one you set up specifically, and it persists all necessary files for reinstallation from local storage.

How do I know if my packages are being installed from the offline cache?

To ensure Yarn is using the offline cache, you can use the yarn install --offline command. If any required package is missing from the offline cache, Yarn will throw an error, indicating it needs to be downloaded. This way, you’ll know exactly when all dependencies are available locally.

What does the yarn-offline-mirror-pruning setting do?

  • Setting yarn-offline-mirror-pruning to true will automatically clean up outdated packages from the offline cache whenever you install or remove packages. This helps keep your offline cache directory organized and saves disk space by removing unused packages.

Can I use Yarn’s offline cache without internet access at all?

Yes, once all necessary packages are cached locally, you can work fully offline. However, to initially set up the offline cache, you will need internet access to download the packages the first time.