@cdwr/nx-payload
Adding support for Payload in your Nx workspace.
Contents
- Prerequisites
- Installation
- Generate a Payload application
- Inferred targets
- Next.js Version
- Changing the database
- Developer Experience (DX)
- You don't have an Nx workspace?
- Versions Compatibility
Prerequisites
- An Nx workspace, version 22 to 24 (see Versions Compatibility)
- Node 22 or 24
- Docker, for the
dx:targets
The commands in this readme assume that Nx is installed globally. If you prefer not to, prefix them with your package manager, for example
npx nx [...].
Installation
This documentation is for a Payload v3 setup. For Payload v2, use plugin version 1.x. For Nx 20 and 21, use plugin version 3.x.
nx add @cdwr/nx-payload
This installs the plugin and registers it in nx.json, unless inference is turned off (see Opt out from automatic inference). No Nx workspace yet? See below.
Generate a Payload application
nx g @cdwr/nx-payload:app [name]
The generator is application, alias app. It creates a Payload application on Next.js, with a Dockerfile and a docker-compose.yml. init and preset also exist, but are internal.
| Option | Type | Required | Default | Description |
|---|---|---|---|---|
name |
string | The name of the application. Can be given as first argument. | ||
directory |
string | The path to the new application. | ||
database |
string | mongodb |
Database adapter to set up: mongodb or postgres. |
|
e2eTestRunner |
string | none |
Test runner for e2e tests: playwright or none. |
|
linter |
string | eslint |
The tool to use for running lint checks: eslint or none. |
|
unitTestRunner |
string | jest |
Test runner for unit tests: jest or none. |
|
tags |
string | Comma separated tags for the project (alias -t). |
||
skipFormat |
boolean | false |
Skip formatting the application files. |
Inferred targets
The plugin has no executors. It infers targets for every project that has a payload.config.ts and a project.json. The default location of the config is {projectRoot}/src.
| Target | What it does |
|---|---|
gen |
Generates TypeScript types and, unless GraphQL is disabled, the GraphQL schema. |
payload |
Runs a native Payload CLI command, for example nx payload [app-name] migrate:status. |
payload-graphql |
Runs a native Payload GraphQL CLI command. |
dx:mongodb |
Starts a MongoDB container in Docker. |
dx:postgres |
Starts a Postgres container in Docker. |
dx:start |
Starts the Payload application and both databases with docker compose. |
dx:stop |
Stops the containers started by dx:start. |
The dev, build and start targets come from @nx/next.
Show all targets of an application:
nx show project [app-name]
Configuration
The plugin is registered in nx.json. Every option is a target name and can be changed. These are the defaults.
{
"plugins": [
{
"plugin": "@cdwr/nx-payload/plugin",
"options": {
"generateTargetName": "gen",
"payloadTargetName": "payload",
"payloadGraphqlTargetName": "payload-graphql",
"dxMongodbTargetName": "dx:mongodb",
"dxPostgresTargetName": "dx:postgres",
"dxStartTargetName": "dx:start",
"dxStopTargetName": "dx:stop"
}
}
]
}
Opt out from automatic inference
To write explicit targets to project.json instead, use one of these:
- Set
useInferencePluginsinnx.jsontofalse - Set the environment variable
NX_ADD_PLUGINStofalse
Next.js Version
This plugin installs Next.js v16 when generating a new Payload application, unless your workspace already has a Next.js version installed.
Next.js v16 is not required — Next.js v15 still works. The plugin installs Payload v3.90, whose @payloadcms/next package supports both Next.js v16 (>=16.3.3) and the patched Next.js v15 lines (15.2.9+, 15.3.9+ or 15.4.11+; not 15.5). v16 is simply the new default for fresh installs.
Can I stay on Next.js v15?
Yes. If your workspace already has Next.js v15 installed, the generator will not upgrade or downgrade it — it only adds Next.js when none is present. It has to be on a line the installed Payload release supports: 15.2.9+, 15.3.9+ or 15.4.11+. Next.js 15.5 is not supported.
Notes
- Payload v3.90 (installed by this plugin) is required for Next.js v16 support
- Next.js v16 builds with Turbopack by default; the generated
next.config.mjsis compatible with it
See this Payload PR related to their work on supporting Next.js v16
Changing the database
Payload has official support for database adapters MongoDB, Postgres and SQLite.
Supabase is set up using the Postgres adapter
Changing the adapter for a generated application must be done manually in payload.config.ts.
We don't want to infer opinionated complexity into the Payload configuration. A new application is just a working template that you will customize and evolve to your needs.
Fortunately, changing the database is straightforward, and only a few parts need to be replaced.
// MongoDB @ payload.config.ts
import { mongooseAdapter } from '@payloadcms/db-mongodb';
export default buildConfig({
db: mongooseAdapter({
url: process.env.DATABASE_URI
})
});
// Postgres/Supabase @ payload.config.ts
import { postgresAdapter } from '@payloadcms/db-postgres';
export default buildConfig({
db: postgresAdapter({
pool: {
connectionString: process.env.DATABASE_URI
}
})
});
More information can be found on the official Payload Database page.
Developer Experience (DX)
The application comes with a set of opinionated targets to improve developer experience. They are prefixed with dx: and optional to use.
Start Payload and database in Docker
This is the quickest way to get Payload up and running in no time.
Using docker compose, MongoDB and Postgres each start in their own container, together with the Payload application.
nx dx:start [app-name]
Open your browser and navigate to http://localhost:3000 to setup your first user.
Stop
Shutdown database and Payload containers.
nx dx:stop [app-name]
Database volumes are persistent, hence all data is available on next launch.
Start a local database instance of choice
You can also start the preferred database first, to be properly initialized before Payload is served.
MongoDB
Run MongoDB in Docker
nx dx:mongodb [app-name]
Postgres
Run Postgres in Docker
nx dx:postgres [app-name]
Supabase
Supabase has its own powerful toolset running local dev with CLI
npx supabase init
npx supabase start
Edit DATABASE_URI in .env.local when needed.
Serve Payload application in development mode
Payload application is served in watch mode.
The configured database must have been started, see local database
nx dev [app-name]
Open your browser and navigate to http://localhost:3000.
Run Payload commands
All commands available from Payload can be used for the application via targets payload and payload-graphql.
nx payload [app-name] [payload-command]
This is specially useful for managing migrations, for example to check database migration status.
nx payload [app-name] migrate:status
Generate TypeScript types and GraphQL schema
To provide a better developer experience for client development, the plugin can generate TypeScript types and GraphQL schema files from the Payload configuration.
GraphQL schema will not be generated if
graphQL.disableis set totrueinpayload.config.ts
nx gen [app-name]
The generated files are written to the generated folder.
The types can be distributed to the client developer manually or saved to a shared library in the monorepo.
The
gentarget is actually an alias for the following commands.nx payload [app-name] generate:types nx payload-graphql [app-name] generate:schemaThe generated app includes a
package.jsonwith"type": "module"to ensure the Payload config loads as ESM when running CLI targets on Node ≥ 20.11. If your workspace rootpackage.jsonalready has"type": "module", this file is redundant but harmless.
Troubleshooting
I can't get Payload to start properly with Postgres in prod mode
Using Postgres in development mode enables automatic database synchronization with the Payload collections. But when starting in production mode it's turned off and the database is expected to have been setup with the collections. So when Postgres is started without a database, Payload will encounter errors.
The solution is to have an initial migration ready for Payload to load during startup.
Create a migration
Make sure Postgres is running. Start Payload in development mode to setup your database with the collections.
nx dev [app-name]
Create a migration.
nx payload [app-name] migrate:create
Now Payload will run migrations automatically when starting in production mode.
The property
db.prodMigrationsinpayload.config.tsmust be set for this to work.
pnpm stops with ERR_PNPM_IGNORED_BUILDS
pnpm 10 and later only run the build scripts of dependencies you have approved, and fail the install otherwise. Payload and Next.js depend on packages that have them, such as esbuild and sharp.
When it happens while creating a workspace with the preset, the workspace is created but the preset never runs, so there is no application. Approve the builds in the new workspace and generate the application yourself:
pnpm approve-builds
pnpm install
nx g @cdwr/nx-payload:app [app-name]
When it happens while adding the plugin, approve the builds and run nx add @cdwr/nx-payload again.
pnpm installs an older version than the latest release
pnpm 11 does not install a version published less than a day ago (minimumReleaseAge) and quietly picks the newest one old enough. A new release of the plugin is therefore available a day after it is published. To take it sooner, add it to minimumReleaseAgeExclude in pnpm-workspace.yaml.
You don't have an Nx workspace?
Create one with a Payload application in it:
npx create-nx-payload
See create-nx-payload for more details.
Versions Compatibility
The Nx column is the supported range. Plugin 4.x is tested on Nx 23 only. Nx 22 and 24 are in range, but not tested.
Plugin 4.x installs Payload 3.90.2 and, when the workspace has no Next.js, Next.js ^16.3.6. The table lists the peer ranges, not every working combination: Next.js 16 needs Payload 3.90 or later, and Next.js 15.5 is not supported. See Next.js Version.
| Plugin | Nx | Payload | React | Next.js |
|---|---|---|---|---|
^4.0.0 |
^22.0.0 || ^23.0.0 || ^24.0.0 |
>=3.0.0 <4.0.0 |
^19.0.0 |
>=15.0.0 <17.0.0 |
^3.0.0 |
^20.4.2 || ^21.0.0 || ^22.0.0 || ^23.0.0 |
>=3.0.0 <4.0.0 |
^19.0.0 |
>=15.0.0 <17.0.0 |
^2.2.1 |
22.x |
3.84.1 |
^19.0.0 |
^15.0.0 |
^2.2.0 |
22.x |
~3.42.0 |
^19.0.0 |
^15.0.0 |
^2.1.0 |
21.x |
~3.42.0 |
^19.0.0 |
^15.0.0 |
^2.0.0 |
^20.4.2 |
^3.0.0 |
^19.0.0 |
^15.0.0 |
^1.0.0 |
20.x |
^2.30.3 |
^18.0.0 |
- |
^0.11.0 |
20.x |
^2.30.3 |
^18.0.0 |
- |
^0.10.0 |
19.x |
^2.8.2 |
- | - |
^0.9.5 |
^19.5.7 |
^2.8.2 |
- | - |
^0.9.0 |
^19.0.2 |
^2.8.2 |
- | - |
^0.8.0 |
^18.3.4 |
^2.8.2 |
- | - |
^0.7.0 |
~18.2.2 |
^2.8.2 |
- | - |
^0.6.0 |
~18.1.1 |
^2.8.2 |
- | - |
^0.5.0 |
~18.0.3 |
^2.8.2 |
- | - |
^0.1.0 |
^17.0.0 |
^2.5.0 |
- | - |