中间件无法与 firebase-admin 模块一起使用,因为操作系统模块

问题描述 投票:0回答:1

我有一个大问题。 我目前正在为我的 api 实现身份验证和验证中间件。 但问题是 firebase-admin 使用的模块无法在 nextjs 中运行,因为它们是 Edge Runtime 模块。 现在我总是收到错误:边缘运行时不支持 Node.js 'os' 模块。错误信息。

这是跟踪堆栈:

Server Error

Error: The edge runtime does not support Node.js 'os' module.
Learn More: https://nextjs.org/docs/messages/node-module-in-edge-runtime
This error happened while generating the page. Any console logs will be displayed in the terminal window.
Call Stack
<unknown>
webpack-internal:///(middleware)/./node_modules/next/dist/esm/server/web/globals.js (42)
Object.apply
webpack-internal:///(middleware)/./node_modules/next/dist/esm/server/web/globals.js (42:19)
eval
webpack-internal:///(middleware)/./node_modules/firebase-admin/lib/app/credential-internal.js (38:20)
eval
webpack-internal:///(middleware)/./node_modules/firebase-admin/lib/app/credential-internal.js (44:3)
Object.(middleware)/./node_modules/firebase-admin/lib/app/credential-internal.js
file:/C:/x/x/.next/server/src/middleware.js (1708:1)

我有一个 firebase-admin.ts 文件:

import * as admin from 'firebase-admin';

if (!admin.apps.length) {
  admin.initializeApp({
    credential: admin.credential.cert(JSON.parse(process.env.FIREBASE_SERVICE_ACCOUNT as string)),
  });
}

const firestore = admin.firestore();
const auth = admin.auth();

export { firestore, auth };

我在中间件内部使用 auth 来验证每个 api 请求。 看起来我无法使用 firebase-admin lib,因为它使用 os 模块。

typescript firebase next.js firebase-admin next.js13
1个回答
0
投票

预计依赖于本机 Nodejs API 的代码将无法在 Edge Runtime 上运行。该产品仅基于 Web API。您可以在他们的文档中查看支持和不支持的内容。具体在不支持的API下:

不支持原生 Node.js API。例如,您无法读取或写入文件系统。

Firebase Admin 设计用于在纯 NodeJS 运行时或旨在与 NodeJS 兼容的运行时(例如 Bun)。

© www.soinside.com 2019 - 2024. All rights reserved.