Supabase Deno边缘函数中Plaid客户端axios导出错误及替代方案咨询
解决Deno Supabase边缘函数中Plaid集成的axios兼容性问题
问题分析
你遇到的TypeError: t is not a function确实是Plaid SDK依赖的旧版axios(0.21.4)与Deno的ESM模块系统不兼容导致的。该版本axios内部使用了Deno不支持的CommonJS特性,触发了调用API时的错误。
解决方法
方案1:替换兼容的axios版本
通过esm.sh的别名功能,强制Plaid SDK使用兼容Deno的新版axios。修改你的导入映射:
{ "imports": { "plaid": "https://esm.sh/plaid@15.0.0?alias=axios:axios@1.6.0" } }
此配置会让Plaid使用axios 1.6.0(或其他你验证过的兼容版本)替代旧版依赖,解决模块兼容性问题。
方案2:直接使用原生HTTP请求调用Plaid API
如果替换axios版本仍无法解决,完全可以用Deno原生的fetch API直接调用Plaid所有REST接口,无需依赖官方SDK。以下是调用link_token/create的示例代码:
export default async function handler(req: Request) { const PLAID_CLIENT_ID = Deno.env.get('PLAID_CLIENT_ID'); const PLAID_SECRET = Deno.env.get('PLAID_SECRET'); const PLAID_ENV = Deno.env.get('PLAID_ENV') || 'sandbox'; const baseUrl = PLAID_ENV === 'production' ? 'https://production.plaid.com' : PLAID_ENV === 'development' ? 'https://development.plaid.com' : 'https://sandbox.plaid.com'; const response = await fetch(`${baseUrl}/link/token/create`, { method: 'POST', headers: { 'Content-Type': 'application/json', 'PLAID-CLIENT-ID': PLAID_CLIENT_ID!, 'PLAID-SECRET': PLAID_SECRET! }, body: JSON.stringify({ user: { client_user_id: 'unique-user-id' }, client_name: 'Your App Name', products: ['auth', 'transactions'], country_codes: ['US'], language: 'en' }) }); const data = await response.json(); return new Response(JSON.stringify(data), { status: response.status }); }
这种方式完全绕开第三方SDK的依赖问题,直接遵循Plaid官方API文档定义的请求格式实现,所有Plaid支持的接口都可通过该方式调用。
验证步骤
- 若使用方案1,修改导入映射后重新部署边缘函数,调用
linkTokenCreate测试是否正常返回链接令牌。 - 若使用方案2,确保请求头包含正确的
PLAID-CLIENT-ID和PLAID-SECRET,请求体参数符合Plaid接口要求,测试返回结果是否符合预期。
内容的提问来源于stack exchange,提问作者pete
相关产品推荐
相关产品推荐

