|
| 1 | +## `workerd` specific code |
| 2 | + |
| 3 | +### Configuration |
| 4 | + |
| 5 | +[`workerd`](https://github.com/cloudflare/workerd) is the runtime cloudflare uses to run Workers code. |
| 6 | + |
| 7 | +While the [`nodejs_compat`](https://developers.cloudflare.com/workers/configuration/compatibility-flags/#nodejs-compatibility-flag) flag makes `workerd` mostly compatible with Node.js, |
| 8 | +there are still minor differences. Some packages publish code for different runtimes to account for those differences. For example, `postgres` has [conditional exports](https://nodejs.org/api/packages.html#conditional-exports) in its [`package.json`](https://github.com/porsager/postgres/blob/ad0ed4476e09f41f147859cb5a42971d2b99e9c7/package.json#L8-L13): |
| 9 | + |
| 10 | +```json |
| 11 | +"exports": { |
| 12 | + "types": "./types/index.d.ts", |
| 13 | + "bun": "./src/index.js", |
| 14 | + "workerd": "./cf/src/index.js", |
| 15 | + "import": "./src/index.js", |
| 16 | + "default": "./cjs/src/index.js" |
| 17 | +}, |
| 18 | +``` |
| 19 | + |
| 20 | +With such exports, Node.js applications use either `src/index.js` or `cjs/src/index.js` depending if the app use ESM or CJS. |
| 21 | + |
| 22 | +However we want to use the `workerd` specific entrypoint when using the Cloudflare adapter. |
| 23 | +For that, you need to instruct Next.js not to bundle packages as it would use the node conditions by default. |
| 24 | + |
| 25 | +To do that, add those packages in the `serverExternalPackages` key of your `next.config.ts`: |
| 26 | + |
| 27 | +```ts |
| 28 | +// node.config.ts |
| 29 | +import type { NextConfig } from "next"; |
| 30 | + |
| 31 | +const nextConfig: NextConfig = { |
| 32 | + serverExternalPackages: ["@prisma/client", ".prisma/client", "postgres"], |
| 33 | +}; |
| 34 | + |
| 35 | +import { initOpenNextCloudflareForDev } from "@opennextjs/cloudflare"; |
| 36 | +initOpenNextCloudflareForDev(); |
| 37 | + |
| 38 | +export default nextConfig; |
| 39 | +``` |
| 40 | + |
| 41 | +### Packages known to have `workerd` specific code |
| 42 | + |
| 43 | +- `postgres` |
| 44 | +- `@prisma/client` (and the generated `.prisma/client`) |
| 45 | + |
| 46 | +Please report an issue on [the adapter GH repository](https://github.com/opennextjs/opennextjs-cloudflare/issues) to have packages added to this list. |
0 commit comments