Cloudflare
操作指南
workerd 专项包

workerd 特定代码

配置

workerd (opens in a new tab) 是 Cloudflare 用来运行 Workers 代码的运行时。

虽然 nodejs_compat (opens in a new tab) 标志使 workerd 大部分兼容 Node.js, 但仍存在细微差异。一些包会为不同的运行时发布代码以应对这些差异。例如,postgres 在其 package.json (opens in a new tab) 中有 条件导出 (opens in a new tab)

"exports": {
  "types": "./types/index.d.ts",
  "bun": "./src/index.js",
  "workerd": "./cf/src/index.js",
  "import": "./src/index.js",
  "default": "./cjs/src/index.js"
},

有了这样的导出,Node.js 应用程序会根据应用使用的是 ESM 还是 CJS 来使用 src/index.jscjs/src/index.js

然而,当我们使用 Cloudflare 适配器时,我们希望使用 workerd 特定的入口点。 为此,您需要指示 Next.js 不要打包这些包,因为它默认会使用 node 条件。

为此,请将这些包添加到您的 next.config.tsserverExternalPackages 键中:

// node.config.ts
import type { NextConfig } from "next";
 
const nextConfig: NextConfig = {
  serverExternalPackages: ["@prisma/client", ".prisma/client", "postgres"],
};
 
import { initOpenNextCloudflareForDev } from "@opennextjs/cloudflare";
initOpenNextCloudflareForDev();
 
export default nextConfig;

已知具有 workerd 特定代码的包

  • @libsql/isomorphic-ws
  • @prisma/client(以及生成的 .prisma/client
  • jose
  • postgres
  • react-textarea-autosize

请在 适配器 GH 仓库 (opens in a new tab) 上报告问题,以便将这些包添加到此列表中。