I'm facing a major issue with my API implementation. I am working on integrating an authentication and verification middleware, but the problem arises when using firebase-admin due to its dependencies on Edge Runtime modules that are incompatible with Next.js.
As a result, I keep encountering the following error message: "The edge runtime does not support Node.js 'os' module."
The tracestack for this error is as follows:
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 occurred during page generation. 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)
In my codebase, I have a file named firebase-admin.ts with the following content:
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 };
The middleware relies on the authentication provided by firebase-admin, but due to its usage of the os module, it seems like there is no straightforward way to resolve this issue.