Using Payload outside Next.js

Payload can be used completely outside of Next.js which is helpful in cases like running scripts, using Payload in a separate backend service, or using Payload's Local API to fetch your data directly from your database in other frontend frameworks like SvelteKit, Remix, Nuxt, and similar.

Importing the Payload Config outside of Next.js

Payload provides a convenient way to run standalone scripts, which can be useful for tasks like seeding your database or performing one-off operations.

In standalone scripts, can simply import the Payload Config and use it right away. If you need an initialized copy of Payload, you can then use the getPayload function. This can be useful for tasks like seeding your database or performing other one-off operations.

1
// We are importing `getPayload` because we don't need HMR
2
// for a standalone script. For usage of Payload inside Next.js,
3
// you should always use `import { getPayloadHMR } from '@payloadcms/next/utilities'` instead.
4
import { getPayload } from 'payload'
5
import config from '@payload-config'
6
7
const seed = async () => {
8
// Get a local copy of Payload by passing your config
9
const payload = await getPayload({ config })
10
11
const user = await payload.create({
12
collection: 'users',
13
data: {
14
email: 'dev@payloadcms.com',
15
password: 'some-password'
16
}
17
})
18
19
const page = await payload.create({
20
collection: 'pages',
21
data: {
22
title: 'My Homepage',
23
// other data to seed here
24
}
25
})
26
}
27
28
// Call the function here to run your seed script
29
await seed()

You can then execute the script using payload run. Example: if you placed this standalone script in src/seed.ts, you would execute it like this:

1
payload run src/seed.ts

The payload run command does two things for you:

  1. It loads the environment variables the same way Next.js loads them, eliminating the need for additional dependencies like dotenv. The usage of dotenv is not recommended, as Next.js loads environment variables differently. By using payload run, you ensure consistent environment variable handling across your Payload and Next.js setup.
  2. It initializes tsx, allowing direct execution of TypeScript files manually installing tools like tsx or ts-node.

Troubleshooting

If you encounter import-related errors, you have 2 options:

Option 1: enable swc mode by appending --use-swc to the payload command:

Example:

1
payload run src/seed.ts --use-swc

Note: Install @swc-node/register in your project first. While swc mode is faster than the default tsx mode, it might break for some imports.

Option 2: use an alternative runtime like bun

While we do not guarantee support for alternative runtimes, you are free to use them and disable payloads own transpilation by appending the --disable-transpilation flag to the payload command:

1
bunx --bun payload run src/seed.ts --disable-transpile

You will need to have bun installed on your system for this to work.

Next

REST API