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.js 或 cjs/src/index.js。
但当我们使用 Cloudflare 适配器时,我们希望使用 workerd 特定的入口点。
为此,你需要指示 Next.js 不要打包这些包,因为默认情况下它会使用 node 的条件。
要实现这一点,将这些包添加到 next.config.ts 的 serverExternalPackages 配置中:
// 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)josepostgresreact-textarea-autosize
如需添加其他包到此列表,请在适配器 GitHub 仓库 (opens in a new tab)提交问题。