db-sqlite is pulling drizzle-kit/api into OpenNext Cloudflare output
Describe the Bug
I’m using Payload with @payloadcms/db-sqlite, Next 16, and @opennextjs/cloudflare.
When building with OpenNext Cloudflare, drizzle-kit/api is reachable from the production server module graph. In a minimal reproduction, OpenNext fails while bundling the server function because it cannot resolve the rewritten drizzle-kit/api import:
ERROR: Could not resolve "drizzle-kit-8c53b399dac79e94/api"
The path appears to be:
@payloadcms/db-sqlite
-> @payloadcms/drizzle/sqlite
-> requireDrizzleKit
-> require('drizzle-kit/api')
The specific Payload path I’m looking at is:
// packages/db-sqlite/src/index.ts
import { requireDrizzleKit } from '@payloadcms/drizzle/sqlite'
Then the adapter attaches requireDrizzleKit to the adapter object.
And then:
// packages/drizzle/src/sqlite/requireDrizzleKit.ts
const {
generateSQLiteDrizzleJson,
generateSQLiteMigration,
pushSQLiteSchema,
} = require('drizzle-kit/api')
Since drizzle-kit/api is migration/schema tooling, I would not expect it to be reachable from the normal SQLite runtime adapter graph.
I’m already using withPayload, so this does not appear to be just a missing Next config wrapper issue:
// next.config.ts
import { initOpenNextCloudflareForDev } from '@opennextjs/cloudflare'
import { withPayload } from '@payloadcms/next/withPayload'
import type { NextConfig } from 'next'
const nextConfig: NextConfig = {
turbopack: {},
}
export default withPayload(nextConfig, { devBundleServerPackages: false })
initOpenNextCloudflareForDev()
Payload config is the normal SQLite setup with sqliteAdapter and database credentials supplied by the environment:
db: sqliteAdapter({
client: {
url: databaseUrl,
authToken: databaseAuthToken,
},
})
I saw the prior work around bundle size / transitive externalization in #14696 and #14845, so this may be an OpenNext Cloudflare edge case rather than plain next build.
Expected Behavior
I would expect drizzle-kit/api to stay out of the production runtime/server output unless migrations or schema generation are being run.
OpenNext Cloudflare should be able to bundle the production server output without needing migration/schema tooling from drizzle-kit/api.
What I’m unsure about
Is withPayload intended to keep drizzle-kit/api out of OpenNext Cloudflare production output, or should the SQLite adapter avoid importing/attaching requireDrizzleKit in a way that keeps migration tooling reachable from the runtime adapter graph?
Link to the code that reproduces this issue
https://github.com/clearfram3/payloadcms-db-issue
Reproduction Steps
-
Clone the linked reproduction repository.
-
Install dependencies:
pnpm install -
Copy the example environment file:
cp .env.example .env -
Build with OpenNext Cloudflare:
pnpm opennext:build -
The build reaches the OpenNext server bundling phase and fails with errors like:
ERROR: Could not resolve "drizzle-kit-8c53b399dac79e94/api" -
The intermediate Next output also contains
drizzle-kit/apiin the traced server files:rg "drizzle-kit/api|drizzle-kit-.*api|generateSQLiteMigration|pushSQLiteSchema" .next .open-next
Which area(s) are affected?
db: sqlite, plugin: other
Environment Info
Binaries:
Node: 22.18.0
npm: 11.7.0
Yarn: N/A
pnpm: 10.33.0
Relevant Packages:
payload: 3.84.1
next: 16.2.4
@payloadcms/db-sqlite: 3.84.1
@payloadcms/drizzle: 3.84.1
@payloadcms/email-nodemailer: 3.84.1
@payloadcms/graphql: 3.84.1
@payloadcms/next/utilities: 3.84.1
@payloadcms/payload-cloud: 3.84.1
@payloadcms/richtext-lexical: 3.84.1
@payloadcms/translations: 3.84.1
@payloadcms/ui/shared: 3.84.1
react: 19.2.0
react-dom: 19.2.0
Operating System:
Platform: darwin
Arch: arm64
Version: Darwin Kernel Version 24.3.0: Thu Jan 2 20:24:23 PST 2025; root:xnu-11215.81.4~3/RELEASE_ARM64_T8122
Available memory (MB): 16384
Available CPU cores: 8
+1; I got the exact same behavior when deploying to Workers using OpenNext, Payload 3.84.1 + NextJS 16.2.4 though im using the @payloadcms/db-d1-sqlite adapter, same error
PS : amazing issue write-up, it has everything
temporary workaround for me has been to add @payloadcms/drizzle/sqlite to the serverExternalPackages option in next.config.ts.
temporary workaround for me has been to add
@payloadcms/drizzle/sqliteto theserverExternalPackagesoption innext.config.ts.
thanks for the tip @francoiscote ! worked perfect, but then I had to also update @opennext because I had this error in runtime (not during build) Unexpected loadManifest(/.next/server/prefetch-hints.json) call!
After updating the @opennext version to latest (thanks to https://github.com/opennextjs/opennextjs-cloudflare/issues/1157), I had a new build error on jose from payload
<head></head>
✘ [ERROR] Could not resolve "jose"
--
09:01:06.652 |
09:01:06.652 | .open-next/server-functions/default/node_modules/payload/dist/auth/operations/me.js:1:26:
09:01:06.652 | 1 │ import { decodeJwt } from 'jose';
I knew there would be errors after @opennext upgrade when I saw this release that added changes to the way serverExternalPackages is handled : https://github.com/opennextjs/opennextjs-cloudflare/releases/tag/%40opennextjs%2Fcloudflare%401.19.2
Anywho, don't know if it was the good thing to do, but I also added jose to the serverExternalPackages and all seems to work now !
Has the latest 16.3 canary or opennext releases fixed this for anyone?
Only solution for me was using webpack and
serverExternalPackages: ["@payloadcms/db-d1-sqlite", "jose", "drizzle-kit"]
Not really perfect, so hoping there'll be a fixed relatively soon. Payload on OpenNext/Cloudflare seems a bit all over the place to me.
Only solution for me was using webpack and
serverExternalPackages: ["@payloadcms/db-d1-sqlite", "jose", "drizzle-kit"]Not really perfect, so hoping there'll be a fixed relatively soon. Payload on OpenNext/Cloudflare seems a bit all over the place to me.
I got around it as well by using webpack instead of turbo.
Next 16.3 stable was just released.
https://github.com/vercel/next.js/releases/tag/v16.3.0
Would be worth re-evaluating a fix for this.
As far as I know, drizzle, which is currently RC.4, is also supposed to be the live version with RC.5. Hopefully this can be addressed in Payload 4 or even sooner so we can disable webpack/use turbopack.
Unfortunately, not resolved in 16.3.0 stable.
@AndreasGalster2 it's on the list! Looks like there are some changes to Turbopack in here which might help us out. Appreciate you connecting the release to this issue!
tbh ditching v3 and nextjs for v4+tanstack
Any expectation when this may roll out? Beta v4?
Any expectation when this may roll out? Beta v4?
My goal is to continue to work on Cloudflare compatibility as we roll through v4 beta. No hard date for you, but I am actively thinking about this.
#17656 (main) and #17660 (3.x) should fix this issue and will ship in the next v4 beta release / 3.x release.
What Changed
Before, our adapters loaded drizzle-kit/api with require(). Turbopack followed that import all the way into the production server output, so OpenNext tried to bundle drizzle-kit-<hash>/api and failed. Now, Drizzle Kit loads lazily only when schema push or migration generation runs. The bundler won't follow that, so Drizzle Kit stays out of the runtime output . Applies to db-sqlite, db-d1-sqlite, db-postgres, and db-vercel-postgres