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

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

Closed
1 task done
nkzawa opened this issue Apr 18, 2022 · 12 comments · Fixed by #36434
Closed
1 task done

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

nkzawa opened this issue Apr 18, 2022 · 12 comments · Fixed by #36434
Labels
Webpack Related to Webpack with Next.js.

Comments

@nkzawa
Copy link
Contributor

nkzawa commented Apr 18, 2022

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.

@nkzawa nkzawa added the bug Issue was opened via the bug report template. label Apr 18, 2022
@riboher
Copy link

riboher commented Apr 18, 2022

I'm experiencing a similar issue after updating my NextJS app to React v18. I have some packages installed that under the hood make use of native Node APIs such as tls, http2, or fs. When running either next build or next dev, the following output shows up:

./node_modules/@grpc/grpc-js/build/src/call-stream.js
Module not found: Can't resolve 'http2' in './node_modules/@grpc/grpc-js/build/src'

Import trace for requested module:
./node_modules/@grpc/grpc-js/build/src/channel.js
./node_modules/@grpc/grpc-js/build/src/index.js
./node_modules/@product/aker-grpc-client/es2015/index.js
./src/services/grpcServices.ts
./src/services/custom/BrazeService.ts
./src/services/workers/exportCommsEnabledDrivers.worker.ts

./node_modules/@grpc/grpc-js/build/src/channel-credentials.js
Module not found: Can't resolve 'tls' in './node_modules/@grpc/grpc-js/build/src'

Import trace for requested module:
./node_modules/@grpc/grpc-js/build/src/index.js
./node_modules/@product/aker-grpc-client/es2015/index.js
./src/services/grpcServices.ts
./src/services/custom/BrazeService.ts
./src/services/workers/exportCommsEnabledDrivers.worker.ts

./node_modules/@grpc/grpc-js/build/src/http_proxy.js
Module not found: Can't resolve 'tls' in './node_modules/@grpc/grpc-js/build/src'

Import trace for requested module:
./node_modules/@grpc/grpc-js/build/src/channel.js
./node_modules/@grpc/grpc-js/build/src/index.js
./node_modules/@product/aker-grpc-client/es2015/index.js
./src/services/grpcServices.ts
./src/services/custom/BrazeService.ts
./src/services/workers/exportCommsEnabledDrivers.worker.ts

./node_modules/@grpc/grpc-js/build/src/resolver-dns.js
Module not found: Can't resolve 'dns' in './node_modules/@grpc/grpc-js/build/src'

Import trace for requested module:
./node_modules/@grpc/grpc-js/build/src/resolver.js
./node_modules/@grpc/grpc-js/build/src/index.js
./node_modules/@product/aker-grpc-client/es2015/index.js
./src/services/grpcServices.ts
./src/services/custom/BrazeService.ts
./src/services/workers/exportCommsEnabledDrivers.worker.ts

./node_modules/@grpc/grpc-js/build/src/resolver-dns.js
Module not found: Can't resolve 'net' in './node_modules/@grpc/grpc-js/build/src'

Import trace for requested module:
./node_modules/@grpc/grpc-js/build/src/resolver.js
./node_modules/@grpc/grpc-js/build/src/index.js
./node_modules/@product/aker-grpc-client/es2015/index.js
./src/services/grpcServices.ts
./src/services/custom/BrazeService.ts
./src/services/workers/exportCommsEnabledDrivers.worker.ts


> Native Node.js APIs are not supported in the Edge Runtime. Found `http2` imported.

Downgrading to react v17 solves the problem

@joecarney
Copy link

@riboher I have just had the same problem and found this issue. When I import a function that I don't call further down, I get Module not found: Can't resolve 'fs'. If I call the function, the error message disappears.

I can remember have had similar problems with this in the past

@balazsorban44 balazsorban44 added kind: bug Webpack Related to Webpack with Next.js. and removed bug Issue was opened via the bug report template. labels Apr 25, 2022
@aruke
Copy link

aruke commented Apr 27, 2022

Is there any workaround other than downgrading React to v17?

@kimbaudi
Copy link

I also get this error message due to importing 'fs' (i.e., import { accessSync, constants } from 'fs';). But isn't that the expected behavior? https://nextjs.org/docs/api-reference/edge-runtime#unsupported-apis notes that Native Node.js APIs are not supported. For example, you can't read or write to the filesystem.

@G4LH4
Copy link

G4LH4 commented Apr 30, 2022

I get this error too while trying to use twilio video, and downgrading React to v17 didn't help. Any ideas?

@nkzawa
Copy link
Contributor Author

nkzawa commented 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.

@marek-hanzal
Copy link

I've the same problem when switched to React 18. I'm using custom entry points for workers which worked fine before switching to R18. Now, the build is failing on node.js native stuff too (like the others here), for example because of Prisma Client.

image

In general, I don't care about some "Edge Runtime" in which case I even don't know what it is or why it even exists or why it's so aggresively pushed inside the build (for Vercel or some serverless stuff, I'm ok), but I'm doing standard build for standard node.js env. so I expect the build to work as standard too, not like this:

image

@kodiakhq kodiakhq bot closed this as completed in #36434 May 2, 2022
kodiakhq bot pushed a commit that referenced this issue May 2, 2022
- improve the message for importing node builtin module on edge runtime
- fix to show the message on overlay of error browser with `next dev`
- fix #36237

The message is NOT shown when using edge runtime (not middleware) since I cannot find a way to detect a webpack compilation is for edge runtime.

## Bug

- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Errors have helpful link attached, see `contributing.md`

## Feature

- [ ] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR.
- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Documentation added
- [ ] Telemetry added. In case of a feature if it's used or not.
- [ ] Errors have helpful link attached, see `contributing.md`

## Documentation / Examples

- [ ] Make sure the linting passes by running `yarn lint`
@CGamesPlay
Copy link
Contributor

CGamesPlay commented 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.

CGamesPlay added a commit to CGamesPlay/next.js that referenced this issue May 4, 2022
This variable is exported by next but undocumented, and its usage is necessary to build auxiliary scripts for your server-side applications. See vercel#36237 (comment) for an example.
kodiakhq bot pushed a commit that referenced this issue May 4, 2022
This variable is exported by next but undocumented, and its usage is necessary to build auxiliary scripts for your server-side applications. See #36237 (comment) for an example.



## Bug

- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Errors have helpful link attached, see `contributing.md`

## Feature

- [ ] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR.
- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Documentation added
- [ ] Telemetry added. In case of a feature if it's used or not.
- [ ] Errors have helpful link attached, see `contributing.md`

## Documentation / Examples

- [ ] Make sure the linting passes by running `yarn lint`
@riboher
Copy link

riboher commented May 5, 2022

Hey @CGamesPlay, thank you for this! It fixed my issue as well! In my case, I had to actually check that the runtime was nodejs in order to apply the config that did use Node APIs, tried again to build and it succeeded. Thanks again!

@marek-hanzal
Copy link

Yep, I can confirm I've solved that issue same way. It was quite hidden there. But now it works.

@balazsorban44
Copy link
Member

balazsorban44 commented May 9, 2022

Docs should be updated #36685, thanks to @CGamesPlay!

@github-actions
Copy link
Contributor

github-actions bot commented Jun 8, 2022

This closed issue has been automatically locked because it had no new activity for a month. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jun 8, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Webpack Related to Webpack with Next.js.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

9 participants