Supabase Edge Functions本地开发出现Function not found报错求助
Supabase Edge Functions 404 "Function not found" 问题排查
问题背景
操作流程如下:
- 执行命令
supabase start启动本地开发环境,成功输出以下信息:
Seeding data supabase/seed.sql... Started supabase local development setup. API URL: http://127.0.0.1:54321 GraphQL URL: http://127.0.0.1:54321/graphql/v1 DB URL: postgresql://postgres:postgres@127.0.0.1:54322/postgres Studio URL: http://127.0.0.1:54323 Inbucket URL: http://127.0.0.1:54324 JWT secret: super-secret-jwt-token-with-at-least-32-characters-long anon key: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZS1kZW1vIiwicm9sZSI6ImFub24iLCJleHAiOjE5ODM4MTI5OTZ9.CRXP1A7WOeoJeXxjNni43kdQwgnWNReilDMblYTn_I0 service_role key: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZS1kZW1vIiwicm9sZSI6InNlcnZpY2Vfcm9sZSIsImV4cCI6MTk4MzgxMjk5Nn0.EGIM96RAZx35lJzdJsyH-qQwv8Hdp7fsn3W0YpN81IU
- 执行命令
supabase functions serve hello,提示Edge Functions运行时已设置,服务地址为http://127.0.0.1:54321/functions/v1/<function-name> - 使用curl命令测试时返回404 Not Found,错误信息为“Function not found”,测试命令:
curl -i --location --request POST 'http://127.0.0.1:54321/functions/v1/hello' \ --header 'Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZS1kZW1vIiwicm9sZSI6ImFub24iLCJleHAiOjE5ODM4MTI5OTZ9.CRXP1A7WOeoJeXxjNni43kdQwgnWNReilDMblYTn_I0' \ --header 'Content-Type: application/json' \ --data '{"name":"Functions"}'
可能的原因及解决方法
1. 函数目录结构或文件内容错误
- 确认项目根目录下存在
supabase/functions/hello文件夹,且文件夹内有index.ts(或index.js)文件 - 检查函数文件是否正确导出默认处理函数,示例代码:
export default function handler(req: Request) { return new Response(JSON.stringify({ message: `Hello ${(await req.json()).name}` }), { headers: { 'Content-Type': 'application/json' }, }); }
2. 函数服务启动异常
- 确保在项目根目录(包含
supabase文件夹的目录)执行supabase functions serve hello命令 - 查看命令启动日志,确认是否有
hello函数加载成功的提示,比如类似Serving function hello at http://127.0.0.1:54321/functions/v1/hello的输出 - 如果启动时有报错,根据错误提示修复(比如依赖缺失、语法错误等)
3. curl命令格式错误
原命令中反斜杠后存在空格,会导致shell解析错误,请求可能被截断。修正后的命令:
curl -i --location --request POST 'http://127.0.0.1:54321/functions/v1/hello' \ --header 'Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZS1kZW1vIiwicm9sZSI6ImFub24iLCJleHAiOjE5ODM4MTI5OTZ9.CRXP1A7WOeoJeXxjNni43kdQwgnWNReilDMblYTn_I0' \ --header 'Content-Type: application/json' \ --data '{"name":"Functions"}'
注意:反斜杠后不能有空格,确保命令行正确换行。
4. 本地环境同步问题
- 尝试先执行
supabase stop停止服务,再重新启动supabase start和supabase functions serve hello - 确认54321端口未被其他服务占用,可使用
lsof -i :54321(Mac/Linux)或netstat -ano | findstr :54321(Windows)检查端口占用情况
内容的提问来源于stack exchange,提问作者Dg Foo
相关产品推荐
相关产品推荐

