Use Remote Caching

By default Nx caches task computations locally. However, to benefit from the cache across your team and in particular on CI, the computation cache can also be distributed across multiple machines. Nx Cloud is an app that provides a fast and zero-config implementation of distributed caching. It is a commercial add-on to Nx, is completely free for OSS projects and comes with generous plans for startups and dedicated offerings for enterprise customers (read more here).

Diagram showing Teika sharing his cache with CI, Kimiko and James

In this diagram, Teika runs the build once on his machine, then CI, Kimiko and James can use the cached artifact from Teika instead of re-executing the same work.

Setup Remote Caching with Nx Cloud

To enable remote caching for your Nx workspace run the following command:

npx nx connect

This connects your workspace with Nx Cloud's remote caching service. It will also allow you to benefit from other Nx Cloud features such as distributed task execution.

To see the remote cache in action, run:

~/workspace

nx build header && nx reset && nx build header

1> nx run header:build 2 3> header@0.0.0 build 4> rimraf dist && rollup --config 5 6src/index.tsx → dist... 7created dist in 786ms 8 9 ————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————— 10 11 > NX Successfully ran target build for project header (2s) 12 13 See logs and investigate cache misses at https://cloud.nx.app/runs/k0HDHACpL8 14 15 16 > NX Resetting the Nx workspace cache and stopping the Nx Daemon. 17 18 This might take a few minutes. 19 20 21 > NX Daemon Server - Stopped 22 23 24 > NX Successfully reset the Nx workspace. 25 26 27> nx run header:build [remote cache] 28 29 30> header@0.0.0 build 31> rimraf dist && rollup --config 32 33 34src/index.tsx → dist... 35created dist in 786ms 36 37 ————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————— 38 39 > NX Successfully ran target build for project header (664ms) 40 41 Nx read the output from the cache instead of running the command for 1 out of 1 tasks. 42 43 Nx Cloud made it possible to reuse header: https://nx.app/runs/P0X6ZGTkqZ 44

Claim your Nx Cloud Workspace

During the setup process you might have seen a link to claim your Nx Cloud connected workspace.

1> NX NOTE Nx Cloud has been enabled 2 3 Your workspace is currently public. Anybody with code access 4 can view the workspace on nx.app. 5 6 You can connect the workspace to your Nx Cloud account at 7 https://nx.app/orgs/workspace-setup?accessToken=N2Y3NzcyO... 8 (You can do this later.) 9

Click on this link to associate the workspace with your Nx Cloud account. If you don't have an Nx Cloud account, you can create one on the spot.

Nx Cloud Workspace Dashboard

Claiming your workspace allows you to

  • see stats about your CI runs, cache hits number of agents used for distributing tasks
  • enable source control integrations to get information embedded in your GitHub, Bitbucket or GitLab PRs
  • manage and create access tokens and adjust access and permission
  • manage your organization & user permissions for your Nx Cloud workspace

If you lose this link, you can still connect your workspace to Nx Cloud. Go to nx.app, create an account, and connect your workspace using the access token from nx.json.

Skipping Cloud Cache

Similar to how --skip-nx-cache will instruct Nx not to use the local cache, passing --no-cloud will tell Nx not to use the remote cache from Nx Cloud.