部署Next.js至Cloudflare Pages时,如何排除Supabase文件夹或解决编译错误?
解决Cloudflare Pages部署Next.js时Supabase Edge函数的编译错误
你在把Next.js应用部署到Cloudflare Pages时,碰到了Supabase Edge函数的编译问题:构建过程扫描到supabase文件夹里的Deno代码,没法解析JSR模块jsr:@supabase/supabase-js@2,导致npm run build失败。你试过.cfignore但没用,这里给两个可行的解决办法:
错误信息
17:01:51.638 ▲ Creating an optimized production build ... 17:02:11.266 ▲ ✓ Compiled successfully 17:02:11.270 ▲ Linting and checking validity of types ... 17:02:14.705 ▲ Failed to compile. 17:02:14.706 ▲ ./supabase/functions/executeTrainings/index.ts:7:30 17:02:14.706 ▲ Type error: Cannot find module 'jsr:@supabase/supabase-js@2' or its corresponding type declarations. 17:02:14.706 ▲ 17:02:14.706 ▲ 5 | // Setup type definitions for built-in Supabase Runtime APIs 17:02:14.706 ▲ 6 | import "jsr:@supabase/functions-js/edge-runtime.d.ts"; 17:02:14.706 ▲ > 7 | import { createClient } from "jsr:@supabase/supabase-js@2"; 17:02:14.706 ▲ | ^ 17:02:14.707 ▲ 8 | import { DateTime } from "https://esm.sh/luxon"; 17:02:14.707 ▲ 9 | 17:02:14.707 ▲ 10 | Deno.serve(async (req) => { 17:02:14.765 ▲ Error: Command "npm run build" exited with 1 17:02:14.806 17:02:14.807 ⚡️ The Vercel build (`npx vercel build`) command failed. For more details see the Vercel logs above.
方案1:让Next.js跳过supabase文件夹
.cfignore只管Cloudflare部署时忽略文件,但Next.js的构建流程还是会扫项目里的所有TS/JS文件。得在next.config.js里加排除规则:
- 打开项目根目录的
next.config.js,添加experimental.excludeFile配置:
/** @type {import('next').NextConfig} */ const nextConfig = { experimental: { excludeFile: (filePath) => { // 排除supabase文件夹下所有文件 return filePath.includes('supabase/'); }, }, }; module.exports = nextConfig;
- 如果你的Next.js版本不支持
excludeFile,可以用transpilePackages反向控制,只处理你需要的Next.js源码目录(比如app或pages):
const nextConfig = { transpilePackages: ['app', 'components'], // 只编译这些目录 }; module.exports = nextConfig;
这样Next.js构建时就不会碰supabase里的Deno代码,自然不会出现模块解析错误。
方案2:彻底隔离Supabase Edge函数
Supabase Edge函数是基于Deno的,本来就不该和Next.js的Node.js环境混在一起构建:
- 直接把
supabase文件夹移出Next.js项目根目录,单独用Supabase CLI部署这些Edge函数。 - 如果要留在项目根目录,就在
tsconfig.json里排除该文件夹,避免TypeScript检查:
{ "exclude": ["node_modules", "supabase/**/*"] }
同时,确保Cloudflare Pages部署时只传Next.js的构建产物(比如.next文件夹),可以把supabase/加到.gitignore里(不需要提交到仓库的话),或者在Cloudflare Pages的部署设置里配置忽略路径。
内容的提问来源于stack exchange,提问作者Jan D.M.
相关产品推荐
相关产品推荐

