Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
alveshelio opened this issue Sep 17, 2024 · 2 comments
Labels
bug Something isn't working triage Unseen or unconfirmed by a maintainer yet. Provide extra information in the meantime.

Comments

@alveshelio
Copy link

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.

@alveshelio alveshelio added bug Something isn't working triage Unseen or unconfirmed by a maintainer yet. Provide extra information in the meantime. labels Sep 17, 2024
@Imunal
Copy link

Imunal commented Sep 18, 2024

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
Copy link
Author

Thanks @Imunal.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working triage Unseen or unconfirmed by a maintainer yet. Provide extra information in the meantime.
Projects
None yet
Development

No branches or pull requests

2 participants