Invalid "Native Node.js APIs are not supported in the Edge Runtime" error message
See original GitHub issueVerify canary release
- I verified that the issue exists in Next.js canary release
Provide environment information
Operating System:
Platform: darwin
Arch: x64
Version: Darwin Kernel Version 19.6.0: Tue Feb 15 21:39:11 PST 2022; root:xnu-6153.141.59~1/RELEASE_X86_64
Binaries:
Node: 16.14.0
npm: 8.3.1
Yarn: 1.22.17
pnpm: N/A
Relevant packages:
next: 12.1.6-canary.3
react: 18.0.0
react-dom: 18.0.0
What browser are you using? (if relevant)
No response
How are you deploying your application? (if relevant)
No response
Describe the Bug
When there is a page like the following, next build
shows the error message > Native Node.js APIs are not supported in the Edge Runtime. Found "child_process" imported, on index.
, even when there is no middleware used.
import { spawn } from 'child_process';
export default function Page() {
return <div>ok</div>
}
This is the same for the compilation error using next dev
.
Also, a related problem, but when you use react version 17, this message is NOT shown for middleware importing child_process
.
Expected Behavior
The error message is shown only for edge functions certainly, even with older react version.
To Reproduce
Import the child_process
module on a page, without middleware, with react@18.
Issue Analytics
- State:
- Created a year ago
- Comments:12 (3 by maintainers)
Top Results From Across the Web
Next.js middleware Module not found: Can't resolve 'fs'
The Edge Runtime, which is used by Next.js Middleware, does not support Node.js native APIs. From the Edge Runtime documentation:.
Read more >Using Node.js Modules in Edge Runtime - Next.js
The code in your Middleware or your Edge API Routes is using a feature from Node.js runtime. However, the Edge Runtime does not...
Read more >Errors | Node.js v19.3.0 Documentation
Indicates that a program is not valid JavaScript. These errors may only be generated and propagated as a result of code evaluation. Code...
Read more >Error messages - Resource Manager - Google Cloud
The request failed because it did not match the specified API. Check the value of the URL path to make sure it is...
Read more >TypeError - JavaScript - MDN Web Docs
The TypeError object represents an error when an operation could not be performed, typically (but not exclusively) when a value is not of ......
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Apologies for commenting here, but @marek-hanzal’s issue is also my issue and I was able to solve it.
The “edge runtime” is a third webpack config that Next is using. This means that to add an extra entry to your webpack config, you need to use the
nextRuntime
hidden option (it should be documented here but isn’t yet). The following seems to work for me:To explain, this function gets called with
isServer: false
once, thenisServer: true, nextRuntime: 'nodejs'
, then withisServer: true, nextRuntime: 'edge'
. The third call is for the edge server bundle.To clarify, basically you cannot use Node.js modules on middleware (edge functions), so it’s expected that
fs
or any other modules don’t work. tho Some node.js modules MIGHT work if they don’t use node.js specific APIs.This issue is about displaying the error message on wrong conditions.