Cloudflare D1 with kysely binding not available during build time

I'm using Kysely to integrate Cloudflare D1 database into my nextjs app. The binding is not working in build time since it need the cloudflareContext env that is not available if the server is not running. In preview it simply fails to make the query, since the db variable is undefined. I've been reading documentations for days and i've tried a lot of solutions. I choose cloudflare D1 because it is supposed to be easy ... I always used this database for my data storage solutions and I never had any issue before using BetterAuth . Also, if I try to create the database migrations with the cli (npx @better-auth/cli generate) it simply fails saying i need to call initOpenNextCloudflareForDev() in my next.config.ts but the function is there! Can someone show me how to configure and export correctly the database instance ?
1 Reply
lonelyplayer3
lonelyplayer3OP3mo ago
npx @better-auth/cli generate 2025-07-07T09:33:53.819Z ERROR [Better Auth]: [#better-auth]: Couldn't read your auth config. Error: ERROR: getCloudflareContext has been called without having called initOpenNextCloudflareForDev from the Next.js config file. You should update your Next.js config file as shown below: ``` // next.config.mjs import { initOpenNextCloudflareForDev } from "@opennextjs/cloudflare"; initOpenNextCloudflareForDev(); const nextConfig = { ... }; export default nextConfig; ``` next js config file import type { NextConfig } from "next"; const nextConfig: NextConfig = { reactStrictMode: true, }; export default nextConfig; // added by create cloudflare to enable calling getCloudflareContext() in next dev import { initOpenNextCloudflareForDev } from '@opennextjs/cloudflare'; initOpenNextCloudflareForDev();

Did you find this page helpful?