Newsletter
Join the Community
Subscribe to our newsletter for the latest news and updates
Learn to implement a monorepo with a single Next.js site using Nx.
Nx Monorepo with Next.js is a Vercel-published example that demonstrates how to structure a single Next.js application inside an Nx workspace, complete with one-click deployment options and development commands tuned for Vercel.
This example, from the Vercel examples repository, walks through creating a monorepo where a single Next.js site lives in the apps/app directory. It uses Nx as the build system and @nrwl/react plugins for generating React applications, libraries, and components. The repo is bootstrapped with create-next-app and can be deployed to Vercel via a one-click clone that sets the output directory to out/.next and runs npx nx build app --prod.
apps/app, specifies the output directory as out/.next, and uses npx nx build app --prod as the build command.npx nx serve app for a dev server on http://localhost:4200 with hot reload, or npx nx build app to produce build artifacts in the dist/ directory.nx g @nrwl/react:component my-component --project=app, new applications with npx nx g @nrwl/react:app new-app, and new libraries with npx nx g @nrwl/react:lib my-lib.npx nx test app, or use npx nx affected:test to run only tests affected by a change.NX_CACHE_DIRECTORY=/tmp/nx-cache to enable caching.@with-nx/mylib.create-next-app workflow.@nrwl/react generators to add apps and libs to the workspace, then share code between them via package-style imports.npx nx affected:test, only the tests touched by a change execute, which speeds up CI pipelines.NX_CACHE_DIRECTORY is set correctly.Bootstrap the example with npx create-next-app --example https://github.com/vercel/examples/tree/main/solutions/nx-monorepo nx-monorepo using npm, Yarn, or pnpm. Then start the development server with npx nx serve app, build with npx nx build app, and test with npx nx test app. For Vercel, either click the deploy button or configure the project manually with the provided build command and root directory.
cacheDirectory in nx.json to match NX_CACHE_DIRECTORY on Vercel, which adds an extra setup step for existing projects.Turborepo is a direct alternative for monorepo management with its own caching and task runner, and Vercel also publishes a Turborepo-based monorepo example.
Run npx nx serve app from the workspace root. The development server starts at http://localhost:4200 and automatically reloads when you change source files.
Use nx g @nrwl/react:component my-component --project=app for a component, npx nx g @nrwl/react:app new-app for an application, or npx nx g @nrwl/react:lib my-lib for a shared library.
Run npx nx test app to execute all unit tests with Jest. To run only the tests affected by a change, use npx nx affected:test.
Use the one-click deploy button in the example's README, or manually set the root directory to apps/app, the output directory to out/.next, and the build command to cd ../../ && npx nx build app --prod.
Yes, the workspace is configured to work with Nx Cloud. For Vercel deployments, set the NX_CACHE_DIRECTORY environment variable to /tmp/nx-cache to enable caching; older versions of @nrwl/nx-cloud also need matching cacheDirectory in nx.json.
