question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Invalid "Native Node.js APIs are not supported in the Edge Runtime" error message

See original GitHub issue

Verify 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:closed
  • Created a year ago
  • Comments:12 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
CGamesPlaycommented, May 4, 2022

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:

  webpack: (config, { nextRuntime }) => {
    // Undocumented property of next 12.
    if (nextRuntime !== "nodejs") return config;
    return {
      ...config,
      entry() {
        return config.entry().then((entry) => ({
          ...entry,
          cli: path.resolve(process.cwd(), "lib/cli.ts"),
        }));
      },
    };
  },

To explain, this function gets called with isServer: false once, then isServer: true, nextRuntime: 'nodejs', then with isServer: true, nextRuntime: 'edge'. The third call is for the edge server bundle.

1reaction
nkzawacommented, May 2, 2022

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found