nextjs
runtime_error
ai_generated
true
Error: The Edge Runtime does not support Node.js API 'X'. Use 'edge-runtime' compatible APIs.
ID: nextjs/middleware-edge-runtime-node-api
95%Fix Rate
90%Confidence
1Evidence
2023-09-15First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| [email protected] | active | — | — | — |
| [email protected] | active | — | — | — |
| [email protected] | active | — | — | — |
Root Cause
Middleware in Next.js runs on the Edge Runtime, which has a limited set of APIs compared to Node.js. Using Node.js-specific modules like 'fs', 'path', or 'crypto' (without Web Crypto) throws this error.
generic中文
Next.js 中间件在 Edge 运行时上运行,与 Node.js 相比 API 有限。使用 Node.js 特定模块如 'fs'、'path' 或 'crypto'(没有 Web Crypto)会抛出此错误。
Official Documentation
https://nextjs.org/docs/app/building-your-application/routing/middleware#runtimeWorkarounds
-
95% success Move Node.js-dependent logic out of middleware into a separate API route or server component.
Move Node.js-dependent logic out of middleware into a separate API route or server component.
-
85% success Use Web APIs available in Edge Runtime, such as Web Crypto (crypto.subtle) instead of Node.js crypto.
Use Web APIs available in Edge Runtime, such as Web Crypto (crypto.subtle) instead of Node.js crypto.
中文步骤
将依赖 Node.js 的逻辑移出中间件,放入单独的 API 路由或服务器组件中。
使用 Edge 运行时中可用的 Web API,例如 Web Crypto(crypto.subtle)替代 Node.js crypto。
Dead Ends
Common approaches that don't work:
-
95% fail
The Edge Runtime does not have a file system; any import of 'fs' will fail at runtime.
-
90% fail
Dynamic imports also run on the Edge Runtime and are blocked; they don't fall back to Node.js.