You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在Deno中使用Firebase Admin遇500错误,如何解决?

解决Deno中导入firebase-admin时的500内部服务器错误

问题代码

import { serve } from "https://deno.land/std@0.171.0/http/server.ts";
import "https://deno.land/x/xhr@0.2.1/mod.ts";
import { installGlobals } from "https://deno.land/x/virtualstorage@0.1.0/mod.ts";
import admin from 'https://esm.sh/firebase-admin@11.5.0';
installGlobals();
const serviceAccount = Deno.env.get('service_account');

if (!serviceAccount) {
  throw new Error('service account details not available');
}

admin.initializeApp(JSON.parse(serviceAccount));

serve((_req) => {
  admin.database().ref('/test').set('Hi there');

  return new Response("Maybe done?", {
    headers: { "content-type": "text/plain" },
  });
});

报错信息

Import 'https://esm.sh/v103/@firebase/database-compat@0.3.1/deno/standalone.js' failed: 500 Internal Server Error
at https://esm.sh/v103/firebase-admin@11.5.0/deno/database.js:2:901


解决办法

1. 更换firebase-admin版本(非必须,但快速有效)

esm.sh对部分新版本的firebase-admin转换可能存在临时问题,降级到已知兼容的版本即可解决。修改导入语句为:

import admin from 'https://esm.sh/firebase-admin@11.4.1';

2. 使用Deno原生npm导入(推荐方案)

Deno支持直接导入npm包,绕开esm.sh的中间转换,避免其服务端错误。将firebase-admin的导入改为:

import admin from 'npm:firebase-admin@11.5.0';

同时可尝试移除xhr和virtualstorage的导入——npm版本的firebase-admin已适配Deno环境,无需额外全局补丁。

3. 清除Deno缓存

如果是缓存的损坏依赖导致问题,执行以下命令强制重新拉取所有依赖:

deno cache --reload your_script.js

内容的提问来源于stack exchange,提问作者Denno

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.03 22:50:28