firebase-js-sdk icon indicating copy to clipboard operation
firebase-js-sdk copied to clipboard

Firebase in Next.js 14 causes Error: Cannot find module 'undici'

Open codercatdev opened this issue 1 year ago • 2 comments

Operating System

macOS 14.5 (23F79)

Browser Version

Version 1.66.118 Chromium: 125.0.6422.147 (Official Build) (arm64)

Firebase SDK Version

10.12.2

Firebase SDK Product:

Auth, Firestore

Describe your project's tooling

Next.js 14, hosted on vercel

Describe the problem

When accessing pages I am getting a 500 error Cannot find module 'undici'. If I try to statically generate these pages it they build but when accessing live they do not render and throw a hard 500.

image

If I remove the static generation and run only on a function they still throw a 500 but the page actually loads.

image image

Everything worked fine until I introduced firebase/auth into the mix

image

Steps and code to reproduce issue

  1. git clone https://github.com/codercatdev/cms-sanity.git
  2. git checkout no-firebase OR git checkout firebase-no-static OR git checkout firebase-with-static

You will then need to host on Vercel as running locally will not show this issue.

codercatdev avatar Jun 18 '24 16:06 codercatdev

Facing the same issue, I'm using firebase via next-firebase-auth-edge.

The issue happens only on production deployments on vercel, for example it doesn't happen when I make a production build locally.

Even though my logs are full of Error 500:

Error: Cannot find module 'undici'

And in the devtools network panel i see screenshot- 2024-06-19 at 13 26 34@2x

But the page is served properly and functioning as expected!

I don't know if this should be reported for firebase, vercel, or nextjs! but I'm gonna report it on every repo to make sure it gets the needed attention.

If it helps anyone, here's the require stack displayed with the error:

Error: Cannot find module 'undici'
Require stack:
- /var/task/apps/web/.next/server/app/(logged-out)/signup/page.js
- /var/task/node_modules/.pnpm/[email protected]_@[email protected]_@[email protected][email protected]_react-dom@1_w5bu2sx7by3sz6f3kfihanbzuu/node_modules/next/dist/compiled/next-server/server.runtime.prod.js
- /var/task/apps/web/___next_launcher.cjs
- /opt/rust/nodejs.js
    at Module._resolveFilename (node:internal/modules/cjs/loader:1145:15)
    at /var/task/node_modules/.pnpm/[email protected]_@[email protected]_@[email protected][email protected]_react-dom@1_w5bu2sx7by3sz6f3kfihanbzuu/node_modules/next/dist/compiled/next-server/server.runtime.prod.js:11:28979
    at Module._load (node:internal/modules/cjs/loader:986:27)
    at /opt/rust/nodejs.js:1:11508
    at Function.Wt (/opt/rust/nodejs.js:1:11878)
    at Q.e.<computed>.K._load (/opt/rust/nodejs.js:1:11478)
    at Module.require (node:internal/modules/cjs/loader:1233:19)
    at u.require (/var/task/node_modules/.pnpm/[email protected]_@[email protected]_@[email protected][email protected]_react-dom@1_w5bu2sx7by3sz6f3kfihanbzuu/node_modules/next/dist/compiled/next-server/server.runtime.prod.js:11:29195)
    at _require.i.require (/var/task/apps/web/.next/server/chunks/1161.js:22:66784)
    at require (node:internal/modules/helpers:179:18) {
  code: 'MODULE_NOT_FOUND',
  requireStack: [
    '/var/task/apps/web/.next/server/app/(logged-out)/signup/page.js',
    '/var/task/node_modules/.pnpm/[email protected]_@[email protected]_@[email protected][email protected]_react-dom@1_w5bu2sx7by3sz6f3kfihanbzuu/node_modules/next/dist/compiled/next-server/server.runtime.prod.js',
    '/var/task/apps/web/___next_launcher.cjs',
    '/opt/rust/nodejs.js'
  ],
  digest: '1496355909'
}

abusada avatar Jun 19 '24 11:06 abusada

Thank you for bringing this to our attention. I tried implementing firebase via next-firebase-auth-edge but I wasn't able to experience any error 'Cannot find module undici'. Aside from Auth, are you using any other Firebase products? Also, I noticed a similar issue filed in Vercel repo. Does rolling back the version to 14.1.4 works on your end?

jbalidiong avatar Jun 21 '24 10:06 jbalidiong

I have the same issue. Page loads fine but the first load is always a 500 which is poluting the logs. I'm on Next.js v14.3.0-canary.24

SaizFerri avatar Jun 30 '24 16:06 SaizFerri

@SaizFerri Can you test rolling back your version as mentioned above by @jbalidiong?

DellaBitta avatar Jul 02 '24 13:07 DellaBitta

@DellaBitta Issue is that I need at least v14.3.0-canary.24 as that version introduced a critical fix to how cookies are set in the middleware, which I also need for auth. :/

SaizFerri avatar Jul 02 '24 13:07 SaizFerri

Still, if we can pinpoint exactly which version things started to go wrong then we can bring the support case to the Next.js team and ask them what the problem might be.

DellaBitta avatar Jul 02 '24 17:07 DellaBitta

@DellaBitta I see what you mean but that app is running on production so I can't easily downgrade the Next.js version there. I can try on our dev env and keep a look at the logs.

SaizFerri avatar Jul 03 '24 19:07 SaizFerri

Hi @SaizFerri,

that would be very helpful since we can't reproduce it ourselves. Thanks!

DellaBitta avatar Jul 03 '24 19:07 DellaBitta

Hey @codercatdev. We need more information to resolve this issue but there hasn't been an update in 5 weekdays. I'm marking the issue as stale and if there are no new updates in the next 5 days I will close it automatically.

If you have more information that will help us get to the bottom of this, just add a comment!

google-oss-bot avatar Jul 10 '24 01:07 google-oss-bot

Undici remains an issue on Vercel

codercatdev avatar Jul 10 '24 01:07 codercatdev

@DellaBitta I see what you mean but that app is running on production so I can't easily downgrade the Next.js version there. I can try on our dev env and keep a look at the logs.

Hi @SaizFerri, any luck with this?

dlarocque avatar Jul 12 '24 13:07 dlarocque

Related: https://github.com/orgs/vercel/discussions/6900

User fixed this issue by downgrading next: https://github.com/orgs/vercel/discussions/6900#discussioncomment-9821638

dlarocque avatar Jul 12 '24 13:07 dlarocque

Hey @codercatdev. We need more information to resolve this issue but there hasn't been an update in 5 weekdays. I'm marking the issue as stale and if there are no new updates in the next 5 days I will close it automatically.

If you have more information that will help us get to the bottom of this, just add a comment!

google-oss-bot avatar Jul 19 '24 01:07 google-oss-bot

Since there haven't been any recent updates here, I am going to close this issue.

@codercatdev if you're still experiencing this problem and want to continue the discussion just leave a comment here and we are happy to re-open this.

google-oss-bot avatar Jul 26 '24 01:07 google-oss-bot

npm install undici

It just worked for me.

deltaepsilon avatar Aug 17 '24 02:08 deltaepsilon