typescript - התווך לא עובד עם מודול firebase-admin בגלל מודול OS
יש לי בעיה גדולה. אני כרגע מיישם מידלווה לאימות ואישור עבור ה-API שלי. אבל הבעיה היא ש-firebase-admin משתמש במודולים שלא יכולים לפעול ב-nextjs בגלל שהם מודולי Edge Runtime. עכשיו אני תמיד מקבל את ההודעה שגיאה: The edge runtime does not support Node.js 'os' module.
זו עקבת המחסנית:
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 };
אני משתמש באימות בתוך התווך כדי לאמת כל בקשת API. נראה שאני לא יכול להשתמש בספריית firebase-admin כי היא משתמשת במודול os.
תשובה #1
צפוי שקוד שתלוי ב-APIs מקומיים של Node.js לא יפעל על Edge Runtime. המוצר הזה מבוסס על APIs של רשת בלבד. תוכלו לראות מה נתמך ומה לא בתיעוד שלהם. במיוחד תחת APIs שאינם נתמכים:
ממשקי ה-API המקומיים של Node.js אינם נתמכים. לדוגמה, אינך יכול לקרוא או לכתוב למערכת הקבצים.
Firebase Admin נועד לפעול על סביבות ריצה של Node.js בלבד, או סביבות ריצה שמטרתן להיות תואמות ל-Node.js (כגון Bun).