The firebase-admin module encounters compatibility issues with middleware due to the OS Module

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.

Answer №1

If your code relies on native nodejs APIs, it may not function properly on Edge Runtime. This particular product is solely based on web APIs. For a list of supported and unsupported features, refer to their official documentation. Pay close attention to the section on unsupported APIs:

It's important to note that Native Node.js APIs are not compatible. This means tasks like filesystem operations are restricted.

Firebase Admin was specifically crafted for use in pure nodejs runtimes, or those that strive to emulate nodejs behavior (like Bun).

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Enhancing search engine optimization through getServerSideProps() in NextJS

I am grappling with a challenge on my website where I need to display dynamic data (Articles) that changes on an hourly basis. To tackle this issue, I have decided to leverage the getServerSideProps() method provided by NextJS. export async function getSe ...

I'm unable to import correctly using the --compiler option

Having an issue here. I'm trying to bring in the typescript compiler. I used this command: bit import bit.envs/compilers/typescript --compiler Unfortunately, it didn't work. This is the error message: bit import [ids...] import components in ...

What is causing class-validator decorators to delete properties when combined with class-transformer?

I am exploring the use of class-validator and class-transformer for validating API requests in a Next.js API route. Below is a simple API handler setup to showcase this: import { plainToInstance } from 'class-transformer'; import { IsString } fr ...

The method to create a global generic class in TypeScript

Is there a way to globally expose the Hash class? Access Playground here export {} class Hash<K, V> { } declare global { // How can we achieve this? } window.Hash = Hash // Making it globally accessible ...

Tips for creating a hover effect on an icon within a CSS grid

I've just started learning to code and wanted to create a component for previewing/displaying a project I'm working on. I've been attempting to add a hover effect to my links such as the GitHubIcon and LaunchIcon, but so far, it's not w ...

How to utilize FileReader for parsing a JSON document?

I'm currently facing an issue while attempting to read and copy a JSON file uploaded by the user into an array. When using .readAsText(), the returned data includes string formatting elements like \" and \n. Is there a way to utilize FileRe ...

In Angular, a white screen may suddenly appear if the scrolling speed is too fast

My experience has been primarily on Chrome. I've noticed that when I scroll for a long time, the data on the screen disappears briefly and then reappears after a few seconds. Is there a resolution for this problem? Thank you, ...

A Error occurs if ReactQuill is used without defining the document object

Recently, I embarked on a journey with both next.js and ReactQuill. However, upon running yarn build, an unexpected obstacle arose: info Creating an optimized production build - info Compiled successfully - info Linting and checking validity of types - in ...

Issues encountered when setting up a Context Provider in React using TypeScript

I am currently in the process of setting up a Cart context in my React TypeScript project, inspired by the implementation found here: https://github.com/AlexSegen/react-shopping-cart/blob/master/src/contexts/CartContext.js. I'm encountering some conf ...

Why isn't my information populating in the Redux state?

I recently converted my React app to a Next.js app, and I am encountering an issue where the data returned from an Axios call is not being stored in the selectors (they are showing as undefined). I suspect there may be a setup problem during the conversion ...

Quirky sample emblem taking the place of characters and indicators in the assignment

Seeking assistance to resolve an unusual issue I've encountered. While running my Next.js project locally (which includes Sanity and styled components), I noticed that some symbols are being replaced by a strange demo icon (as shown in the image belo ...

Access the array values by their respective keys in an object that is returned from a custom JavaScript file utilizing the Node.js file system

I recently came across a config file with a unique format, as shown below: define([], function () { return { productItems: { item1: ['Apple', 'Ball', 'Car'], item2: [&apo ...

What causes the inconsistency in TypeScript's structure typing?

It is well-known that TypeScript applies structure typing, as demonstrated in the following example: interface Vector { x: number; y: number; } interface NamedVector { x: number; y: number; name: string; } function calculateLength(v: Vecto ...

How to load a PFX certificate from a file in NodeJS

For my current project involving Node.JS and TypeScript, one of the key requirements is to encrypt the payload body using a PFX certificate read from a .pfx file. The certificate I have is named cert1.pfx, and my code necessitates utilizing this certifica ...

Utilizing identical elements on a single page can lead to glitches within Next.js

Update: A codesandbox has been created to demonstrate the issue. There are 3 components that allow file uploads, but the first component always gets updated regardless of which one is chosen. Check out the codesandbox here The latest version of Next.js is ...

Utilize [slug] as the primary index in the following section as well

I'm currently delving into Next.js for a project, and I'm attempting to manage all the routes using just one file. Specifically, utilizing [slug].tsx instead of index.tsx. I gave it a shot but unfortunately it didn't work out as expected. An ...

Incorporating the id attribute into the FormControl element or its parent in Angular 7

I'm attempting to assign an id attribute to the first invalid form control upon form submission using Angular reactive forms. Here is my current component code: onSubmit() { if (this.form.invalid) { this.scrollToError(); } else { ...

The information displayed in Next.js appears in the form of an array, which is distinct from the JSON object provided by the API

I am retrieving data from a Laravel API using getServerSideProps(): This is the method I use to fetch the data: function Product({ data }) { console.log(data); return ( <div>Hello</div> ) } export async function getServerSidePr ...

Adding comments in TypeScript: A quick guide

Hey there, I'm new to TS and could use some help. Here is the code snippet I have: I want to comment out the logo but adding "//" and '/*' doesn't seem to work. This is what I tried: // <LogoComponent classes={{container: style.log ...

NextJS stores user information and token (obtained from an external API) for both client-side and server-side components

I am currently developing a fresh web application in NextJS, incorporating both client-side and server-side components. The user data is being transmitted to an API that already exists (.NET). My task is to store the details of the logged-in user such as t ...