Skip to content

db-sqlite is pulling drizzle-kit/api into OpenNext Cloudflare output #16470

Closed

Description

@clearfram3

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

  1. Clone the linked reproduction repository.

  2. Install dependencies:

    pnpm install
  3. Copy the example environment file:

    cp .env.example .env
  4. Build with OpenNext Cloudflare:

    pnpm opennext:build
  5. The build reaches the OpenNext server bundling phase and fails with errors like:

    ERROR: Could not resolve "drizzle-kit-8c53b399dac79e94/api"
  6. The intermediate Next output also contains drizzle-kit/api in 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

Activity

  1. added
    status: needs-triage
    validate-reproduction
    on May 4, 2026
  2. added
    db: sqlite
    plugin: other
    and removed
    validate-reproduction
    on May 4, 2026
  3. removed
    status: needs-triage
    on May 6, 2026
  4. celestial-rose commented on May 11, 2026

    @celestial-rose

    +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

  5. francoiscote commented on May 14, 2026

    @francoiscote

    temporary workaround for me has been to add @payloadcms/drizzle/sqlite to the serverExternalPackages option in next.config.ts.

  6. celestial-rose commented on May 15, 2026

    @celestial-rose

    temporary workaround for me has been to add @payloadcms/drizzle/sqlite to the serverExternalPackages option in next.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 opennextjs/opennextjs-cloudflare#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 !

  7. clearfram3 commented on May 27, 2026

    @clearfram3
    Author

    Has the latest 16.3 canary or opennext releases fixed this for anyone?

  8. AndreasGalster2 commented on Jul 23, 2026

    @AndreasGalster2

    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.

  9. LVLUP-Studio commented on Jul 23, 2026

    @LVLUP-Studio

    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.

  10. AndreasGalster2 commented on Aug 3, 2026

    @AndreasGalster2

    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.

  11. nathanlentz commented on Aug 4, 2026

    @nathanlentz
    Collaborator

    @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!

  12. clearfram3 commented on Aug 18, 2026

    @clearfram3
    Author

    tbh ditching v3 and nextjs for v4+tanstack

  13. AndreasGalster2 commented on Sep 8, 2026

    @AndreasGalster2

    Any expectation when this may roll out? Beta v4?

  14. nathanlentz commented on Sep 9, 2026

    @nathanlentz
    Collaborator

    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.

  15. nathanlentz commented on Oct 27, 2026

    @nathanlentz
    Collaborator

    #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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

db: sqliteplugin: otherv3

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions