Nx is an extensible build system with support for monorepos, integrations, and Remote Caching on Vercel.
Read the Intro to Nx docs to learn about the benefits of using Nx to manage your monorepos.
To add Nx to an existing monorepo, use the following command, as described in their Getting Started guide.
npx add-nx-to-monorepo
To create a new Next.js monorepo with Nx, use:
npx create-nx-workspace@latest --preset=next.
You can also browse their other supported plugins and guides.
nxversion:14.6.2or laternx-cloudversion:14.6.0or later
When using remote caching with Nx:
- Ensure the
NX_CACHE_DIRECTORY=/tmp/nx-cacheis set
or
- Set the
cacheDirectoryoption to/tmp/nx-cacheattasksRunnerOptions.{runner}.optionsin yournx.json.
For example:
"tasksRunnerOptions": {
"default": {
"runner": "nx/tasks-runners/default",
"options": {
"cacheDirectory": "/tmp/nx-cache"
}
}
}
Create a new Project on the Vercel dashboard and import your monorepo project.
From the Build and Output Settings panel:
- Set the Framework to Next.js (or the framework your application is using)
- Set the Build Command, where
<app>is the directory name of the target application withinapps/, and save your changes.
npx nx build <app> --prod

Setting the Nx build command.
From the Settings panel, set the Output Directory to point to the generated output of <app>. This can vary depending on your apps
project.json outputs setting, but by default will be dist/:
dist/apps/<app>/.next

Setting the Nx output directory.
For Nx projects, leave the root directory unchanged (set to the root of your monorepo where your nx.json can be found).

The root of the monorepo.
To configure Remote Caching for your Nx project on Vercel, use the @vercel/remote-nx plugin.
npm install --save-dev @vercel/remote-nx
In your nx.json file you will find a tasksRunnerOptions field. Update this field so that it's using the installed @vercel/remote-nx:
{
"tasksRunnerOptions": {
"default": {
"runner": "@vercel/remote-nx",
"options": {
"cacheableOperations": ["build", "test", "lint", "e2e"],
"token": "<token>",
"teamId": "<teamId>"
}
}
}
}
You can specify your token and teamId in your nx.json or set them as environment variables.
Parameter | Description | Environment Variable / .env | nx.json |
|---|---|---|---|
Vercel Access Token | Vercel access token with access to the provided team | NX_VERCEL_REMOTE_CACHE_TOKEN | token |
Vercel Team ID (optional) | The Vercel Team ID that should share the Remote Cache | NX_VERCEL_REMOTE_CACHE_TEAM | teamId |
Clear your local cache and rebuild your project.
npx nx reset
npx nx build