nextauthjs / next-auth

Authentication for the Web.
https://authjs.dev
ISC License
24.24k stars 3.37k forks source link

Error: The edge runtime does not support Node.js 'fs' module. in Next middleware #11858

Closed alveshelio closed 3 days ago

alveshelio commented 5 days ago

Environment

System:
    OS: macOS 14.6.1
    CPU: (10) arm64 Apple M1 Pro
    Memory: 63.83 MB / 32.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 20.12.2 - ~/.volta/tools/image/node/20.12.2/bin/node
    npm: 10.5.0 - ~/.volta/tools/image/node/20.12.2/bin/npm
    pnpm: 9.4.0 - ~/.volta/bin/pnpm
    bun: 1.1.20 - ~/.bun/bin/bun
  Browsers:
    Chrome: 128.0.6613.138
    Safari: 17.6
  npmPackages:
    @auth/drizzle-adapter: ^1.5.0 => 1.5.0
    next: 14.2.11 => 14.2.11
    next-auth: ^5.0.0-beta.21 => 5.0.0-beta.21
    react: ^18 => 18.3.1```

Reproduction URL

https://github.com/parite-sciences/webapp-resources

Describe the issue

I'm working on a project with Authjs and I'm using Drizzle and SQLite with better-sqlite3 and when checking if the user is authenticated in the middleware with auth I'm getting this error:

Error: The edge runtime does not support Node.js 'fs' module.
Learn More: https://nextjs.org/docs/messages/node-module-in-edge-runtime
at (webpack-internal:///(middleware)/./node_modules/.pnpm/next@14.2.11_react-dom@18.3.1_react@18.3.1__react@18.3.1/node_modules/next/dist/esm/server/web/globals.js:34)
at Object.get (webpack-internal:///(middleware)/./node_modules/.pnpm/next@14.2.11_react-dom@18.3.1_react@18.3.1__react@18.3.1/node_modules/next/dist/esm/server/web/globals.js:34:19)
at promisify (node:internal/util:400:15)
at eval (webpack-internal:///(middleware)/./node_modules/.pnpm/better-sqlite3@11.3.0/node_modules/better-sqlite3/lib/methods/backup.js:6:18)
at (middleware)/./node_modules/.pnpm/better-sqlite3@11.3.0/node_modules/better-sqlite3/lib/methods/backup.js (file:///.next/server/src/middleware.js:135:1)
at webpack_require (file:///.next/server/edge-runtime-webpack.js:37:33)
at fn (file:///.next/server/edge-runtime-webpack.js:285:21)
at eval (webpack-internal:///(middleware)/./node_modules/.pnpm/better-sqlite3@11.3.0/node_modules/better-sqlite3/lib/database.js:79:29)
at (middleware)/./node_modules/.pnpm/better-sqlite3@11.3.0/node_modules/better-sqlite3/lib/database.js (file:///.next/server/src/middleware.js:102:1)
at webpack_require (file:///.next/server/edge-runtime-webpack.js:37:33)
at fn (file:///.next/server/edge-runtime-webpack.js:285:21)

If I remove auth from the middleware then the error goes away.

How to reproduce

pnpm install && pnpm dev

Expected behavior

I would expect to be able to check if the user is authenticated or not in the middleware.

Imunal commented 5 days ago

It's because Next.js uses Edge runtime in middleware. (https://nextjs.org/docs/app/building-your-application/routing/middleware#runtime

The problem is writed in the docs. https://authjs.dev/guides/edge-compatibility#middleware

alveshelio commented 3 days ago

Thanks @Imunal.