awinogrodzki / next-firebase-auth-edge

Next.js Firebase Authentication for Edge and Node.js runtimes. Compatible with latest Next.js features.
https://next-firebase-auth-edge-docs.vercel.app/
MIT License
499 stars 43 forks source link

Build error in version 1.4.2 `Module not found: Can't resolve 'crypto'` #164

Closed steve-marmalade closed 5 months ago

steve-marmalade commented 6 months ago

Hey there! I just upgraded to the latest release and am getting a build error when deploying to Vercel. I'm using next 14.2.0-canary.52 (and Node.js Version 20.x if that's relevant).

Full stacktrace:

Failed to compile.
./node_modules/.pnpm/next-firebase-auth-edge@1.4.2_next@14.2.0-canary.52/node_modules/next-firebase-auth-edge/lib/digest/node.js:4:1
Module not found: Can't resolve 'crypto'
https://nextjs.org/docs/messages/module-not-found
Import trace for requested module:
./node_modules/.pnpm/next-firebase-auth-edge@1.4.2_next@14.2.0-canary.52/node_modules/next-firebase-auth-edge/lib/digest/index.js
./node_modules/.pnpm/next-firebase-auth-edge@1.4.2_next@14.2.0-canary.52/node_modules/next-firebase-auth-edge/lib/auth/signature-verifier.js
./node_modules/.pnpm/next-firebase-auth-edge@1.4.2_next@14.2.0-canary.52/node_modules/next-firebase-auth-edge/lib/auth/token-verifier.js
./node_modules/.pnpm/next-firebase-auth-edge@1.4.2_next@14.2.0-canary.52/node_modules/next-firebase-auth-edge/lib/auth/index.js
./node_modules/.pnpm/next-firebase-auth-edge@1.4.2_next@14.2.0-canary.52/node_modules/next-firebase-auth-edge/lib/next/tokens.js
./auth/server-auth-provider.tsx
./app/components/products/actions.ts
./node_modules/.pnpm/next@14.2.0-canary.52_@babel+core@7.24.3_@opentelemetry+api@1.7.0_react-dom@18.2.0_react@18.2.0_sass@1.72.0/node_modules/next/dist/build/webpack/loaders/next-flight-action-entry-loader.js?actions=%5B%5B%22%2Fvercel%2Fpath0%2Fapp%2Fcomponents%2Fproducts%2Factions.ts%22%2C%5B%22nominateProduct%22%2C%22%24%24ACTION_0%22%2C%22%24%24ACTION_1%22%2C%22toggleUpvoteProduct%22%5D%5D%2C%5B%22%2Fvercel%2Fpath0%2Fapp%2Fcomponents%2Fnavbar%2Fsearch%2FhandleRecordExploredProduct.ts%22%2C%5B%22default%22%5D%5D%5D&__client_imported__=true!
awinogrodzki commented 6 months ago

Hey @steve-marmalade!

Thanks for reporting.

I am debugging it now. I'll get back to you shortly

awinogrodzki commented 6 months ago

I am unable to reproduce the issue by updating next to 14.2.0-canary.52 in starter example: https://github.com/awinogrodzki/next-firebase-auth-edge/pull/165/files

Could you share some more context:

I assume /app/components/products/actions.ts uses getTokens function. Does it import anything other than getTokens?

Could you share the code where you're calling the action? You can strip irrelevant parts

Also, could you share full logs from Next.js build output log?

Are you using generateStaticParams in the page that leads to the error?

bigxalx commented 6 months ago

Hey I am getting a similar error. I wanted to use the package in a Cloudflare Pages project in a function. This should be just the same as running on a Cloudflare worker. The code is simply:

import { getFirebaseAuth } from "next-firebase-auth-edge";
...
await setCustomUserClaims(...)

Throws this:

✘ [ERROR] Could not resolve "crypto"

    ../node_modules/next-firebase-auth-edge/lib/digest/node.js:4:25:
      4 │ const crypto_1 = require("crypto");
        ╵                          ~~~~~~~~

  The package "crypto" wasn't found on the file system but is built into node.
  Add the "nodejs_compat" compatibility flag to your Pages project and make sure to prefix the module name with "node:" to enable Node.js compatibility.

I could not get the suggested fix with nodejs_compat working yet.

awinogrodzki commented 6 months ago

Hey @bigxalx @steve-marmalade!

I've introduced a fix in v1.4.3.

The issue was caused by digest function that served different implementation (nodejs/web crypto) based on cryptoRuntime variable exposed by jose. I used the function only in debug logs and decided to remove it until I sort this out.

You can install next-firebase-auth-edge@1.4.3 and please let me know if it works! 🍻

bigxalx commented 6 months ago

Just updated, get this error now:

✘ [ERROR] service core:user:worker: Uncaught ReferenceError: __dirname is not defined

    at null.<anonymous> (functionsWorker-0.17500218561564473.js:13707:34)
    at null.<anonymous> (functionsWorker-0.17500218561564473.js:13710:7) in
  ../node_modules/next/dist/compiled/ua-parser-js/ua-parser.js
    at null.<anonymous> (functionsWorker-0.17500218561564473.js:38:50) in
  __require
    at null.<anonymous> (functionsWorker-0.17500218561564473.js:13740:64) in
  ../node_modules/next/dist/server/web/spec-extension/user-agent.js
    at null.<anonymous> (functionsWorker-0.17500218561564473.js:38:50) in
  __require
    at null.<anonymous> (functionsWorker-0.17500218561564473.js:13790:28) in
  ../node_modules/next/server.js
    at null.<anonymous> (functionsWorker-0.17500218561564473.js:38:50) in
  __require
    at null.<anonymous> (functionsWorker-0.17500218561564473.js:16029:20) in
  ../node_modules/next-firebase-auth-edge/lib/next/middleware.js
    at null.<anonymous> (functionsWorker-0.17500218561564473.js:38:50) in
  __require
    at null.<anonymous> (functionsWorker-0.17500218561564473.js:16176:24) in
  ../node_modules/next-firebase-auth-edge/lib/index.js

✘ [ERROR] MiniflareCoreError [ERR_RUNTIME_FAILURE]: The Workers runtime failed to start. There is likely additional logging output above.

On 6. Apr 2024, at 15:27, Amadeusz Winogrodzki @.***> wrote:

Hey @bigxalx https://github.com/bigxalx @steve-marmalade https://github.com/steve-marmalade!

I've introduced a fix in v1.4.3.

The issue was caused by digest function that served different implementation (nodejs/web crypto) based on cryptoRuntime variable exposed by jose. I used the function only in debug logs and decided to remove it until I sort this out.

You can install @.*** and please let me know if it works! 🍻

— Reply to this email directly, view it on GitHub https://github.com/awinogrodzki/next-firebase-auth-edge/issues/164#issuecomment-2041082083, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAD42YXZI45332ZKTP3JK23Y37Z5TAVCNFSM6AAAAABFT4TLKWVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDANBRGA4DEMBYGM. You are receiving this because you were mentioned.

awinogrodzki commented 6 months ago

@bigxalx do you still get the error if you change the middleware to this?

import {NextRequest,NextResponse} from 'next/server';

export async function middleware(request: NextRequest) {
  return NextResponse.next();
}
bigxalx commented 6 months ago

Not using any middleware at all, just importing the package and trying to call setCustomClaims as outlined above.

On 6. Apr 2024, at 15:51, Amadeusz Winogrodzki @.***> wrote:

@bigxalx https://github.com/bigxalx do you still get the error if you change the middleware to this?

import {NextRequest,NextResponse} from 'next/server';

export async function middleware(request: NextRequest) { return NextResponse.next(); } — Reply to this email directly, view it on GitHub https://github.com/awinogrodzki/next-firebase-auth-edge/issues/164#issuecomment-2041088627, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAD42YSIO27ORXHLZ3Y56O3Y374X3AVCNFSM6AAAAABFT4TLKWVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDANBRGA4DQNRSG4. You are receiving this because you were mentioned.

awinogrodzki commented 6 months ago

I got you. What if you change the import to import {getFirebaseAuth} from 'next-firebase-auth-edge/lib/auth';

Does the error still persist?

awinogrodzki commented 5 months ago

I will close the issue now. Feel free to reopen if you still face the issue