next.js: Debugging with NODE_OPTIONS='--inspect=0.0.0.0' next dev causes errors

Verify canary release

  • I verified that the issue exists in the latest Next.js canary release

Provide environment information

Operating System:
      Platform: linux
      Arch: x64
      Version: #1 ZEN SMP PREEMPT_DYNAMIC Tue, 04 Jul 2023 08:39:22 +0000
    Binaries:
      Node: 20.3.1
      npm: 8.19.2
      Yarn: N/A
      pnpm: 8.6.6
    Relevant Packages:
      next: 13.4.12
      eslint-config-next: 13.0.0
      react: 18.2.0
      react-dom: 18.2.0
      typescript: 5.1.6
    Next.js Config:
      output: N/A

Which area(s) of Next.js are affected? (leave empty if unsure)

No response

Link to the code that reproduces this issue or a replay of the bug

https://github.com/vercel/next.js/blame/8d83d85e5359146bbbd0f96ea2f8490e7d6bc9cd/packages/next/src/cli/next-dev.ts#L488

To Reproduce

NODE_OPTIONS=‘–inspect=0.0.0.0’ next dev

Describe the Bug

NodeJS’s --inspect switch takes an optional address as a parameter that isn’t being honored by Next. Next crashes when that is present. spec: --inspect[=[host:]port] https://nodejs.org/api/cli.html#--inspecthostport

In order to be able to expose a debugger from inside a Docker container, it has to listen on 0.0.0.0 instead of the default 127.0.0.1. To do that, we have to specify --inspect=0.0.0.0:9229.

The result of doing this is:

- info the --inspect option was detected, the Next.js proxy server should be inspected at port 0.
- ready started server on 0.0.0.0:3000, url: http://localhost:3000
- info the --inspect option was detected, the Next.js routing server should be inspected at port 1.
/usr/local/bin/node:  must be 0 or in range 1024 to 65535.

This and the root cause has been clearly described earlier in: https://github.com/vercel/next.js/issues/47083 but not fixed, as mentioned by gregmartyn in https://github.com/vercel/next.js/issues/47083#issuecomment-1572453046

This has also been described in (and presumably fixed but not accepted) in https://github.com/vercel/next.js/pull/47671

Expected Behavior

Start node debugger on 0.0.0.0 and default ports 9232 & 9233 instead of parsing “0.0.0.0” as a port only integer. Honor the node --inspect switch parameters.

Which browser are you using? (if relevant)

No response

How are you deploying your application? (if relevant)

No response

About this issue

  • Original URL
  • State: closed
  • Created a year ago
  • Reactions: 15
  • Comments: 20 (2 by maintainers)

Commits related to this issue

Most upvoted comments

Its is a duplication of https://github.com/vercel/next.js/issues/47083. I will create a PR in order to fix this. This should help people debugging an nextjs application that runs inside a docker container

Worth pointing out that the workaround shows how easy this would be to fix. Let’s get the fix in a release.

I also ran into it while trying to debug nextjs in a docker container. node_module/.bin/next dev starts the dev server here. And before that it amends the node options (to make the child bind to the next port, to be able to debug both processes). The thing is, getDebugPort and the “change the options” code (the previous link) handle only the --${nodeDebugType}=PORT case. But there’s also --${nodeDebugType}=HOST:PORT and --${nodeDebugType}=HOST.

Workaround: change this line to:

NODE_OPTIONS = `${NODE_OPTIONS} --${nodeDebugType}=0.0.0.0:9230`;

then:

$ NODE_OPTIONS=--inspect-brk=0.0.0.0 node_modules/.bin/next dev

And another relevant link:

https://github.com/vercel/next.js/blob/v14.0.4-canary.32/packages/next/src/server/lib/start-server.ts#L240-L245

P.S. This must be an anti-reverse-engineering kind of bug 😃

Currently still relevant issue. It’s not possible to propagate debugger from Docker container because by default it listens only on internal IP.