Skip to content

Debugger not binding breakpoints in VSCode in server components, using official Next.js debugger setup #62008

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

Open
andrewmartin opened this issue Feb 13, 2024 · 68 comments
Labels
bug Issue was opened via the bug report template.

Comments

@andrewmartin
Copy link

andrewmartin commented Feb 13, 2024

Link to the code that reproduces this issue

https://github.com/andrewmartin/nextjs-example

To Reproduce

  1. Open the code in VSCode
  2. Add a breakpoint in any server side files
  3. Open the "Run and Debug" panel
  4. Select "Next.js: debug full stack" and press "F5" or the play button
  5. Note that breakpoints don't land when requests are made in either scenario.

Breakpoints set in server components that aren't attached (sorry the screencaps below didn't show my mouse hover so here's a gif):

Zight Recording 2024-02-13 at 09 45 03 AM

page tsx — tmp2 2024-02-13 at 9 36 58 AM
route ts — tmp2 2024-02-13 at 9 38 08 AM

Debug diagnostics:

Debug Diagnostics — tmp2 2024-02-13 at 9 39 02 AM

Some of the sources loaded (I can provide more if needed):

Debug Diagnostics — tmp2 2024-02-13 at 9 39 53 AM

Current vs. Expected behavior

Hi everyone! The code repo shared is a brand new, blank create-next-app using Typescript, Next 14, and the app directory. I only added the debugging setup from the official docs and a single basic API endpoint at /api/health and that's it. For some reason I cannot for the life of me get my breakpoints to attach.

Well, that's partially true, The only breakpoints that seem to be mapped with my codebase are those in the Browser / client side components. I have tried SO many things to get this to work and have read probably about 30 posts about trying to fix this.

I'd love to know if anyone has any ideas why a vanilla Next.js project might not find my sources. Thanks in advance for your help!

Provide environment information

Operating System:
  Platform: darwin
  Arch: arm64
  Version: Darwin Kernel Version 23.3.0: Wed Dec 20 21:30:44 PST 2023; root:xnu-10002.81.5~7/RELEASE_ARM64_T6000
Binaries:
  Node: 20.11.0
  npm: 10.2.4
  Yarn: 1.22.21
  pnpm: N/A
Relevant Packages:
  next: 14.1.0
  eslint-config-next: 14.1.0
  react: 18.2.0
  react-dom: 18.2.0
  typescript: 5.3.3
Next.js Config:
  output: N/A

Which area(s) are affected? (Select all that apply)

Not sure

Which stage(s) are affected? (Select all that apply)

next dev (local)

Additional context

I have tried:

  • Using blank (vanilla) VSCode insiders
  • Using alternative node versions (node 18, other minor node 20)
  • Tried mucking around with the launch config a bit, like adjusting sourceMapPathOverrides etc

Some of my colleagues have had luck seeing them, others have not.

@andrewmartin andrewmartin added the bug Issue was opened via the bug report template. label Feb 13, 2024
@andrewmartin andrewmartin changed the title Debugger examples not working in VSCode on latest version Debugger not binding breakpoints in VSCode in server components, using official Next.js debugger setup Feb 13, 2024
@dmitrc
Copy link

dmitrc commented Feb 14, 2024

I've been facing the same issue, and ultimately the discussion in #56702 is what helped me solve it.

Add the following launch.json:

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "Next.js: debug server-side",
      "type": "node-terminal",
      "request": "launch",
      "command": "npm run dev",
      "sourceMaps": true,
      "sourceMapPathOverrides": {
        "/turbopack/[project]/*": "${webRoot}/*"
      }
    },
    {
      "name": "Next.js: debug client-side",
      "type": "msedge",
      "request": "launch",
      "url": "http://localhost:3000",
      "webRoot": "${workspaceFolder}",
      "sourceMaps": true,
      "sourceMapPathOverrides": {
        "/turbopack/[project]/*": "${webRoot}/*"
      }
    }
  ]
}

If you're not using Turbopack, replace sourceMapPathOverrides with:
(haven't tried this one)

"sourceMapPathOverrides": {
  "webpack://_N_E/*": "${webRoot}/*"
}

I definitely did not need to do this back in the day (13.5.x for sure), so something must have regressed relatively recently.

@andrewmartin
Copy link
Author

Thanks @dmitrc , and thank you for referencing #56702 which I found helpful too

I actually tried this and it still didn't seem to pick things up. Keep in mind I'm specifically working with server files, e.g. route.ts files in this workflow. I've had some (limited) success grabbing Browser breakpoints.

To be doubly clear, I'm hoping to debug against server code in Next.js, not just the browser.

I'm wondering if there's some other path to consider to include or index on the sourcemaps from route.ts files.

@dmitrc
Copy link

dmitrc commented Feb 14, 2024

@andrewmartin, just to confirm, while the other thread is named "client-side debugging", for my case I was actually also looking to fix the issues with debugging server files specifically (page.tsx), and following the steps outlined above helped.

@andrewmartin
Copy link
Author

@dmitrc totally I get it. Thanks much. I still am struggling :(

@dmitrc
Copy link

dmitrc commented Feb 14, 2024

I hope your thread can get some attention from the team -- debugging is such a core part of development experience, it's in everyone's best interest to get this fixed and/or clarified ASAP. Best of luck with your scenario!

@andrewmartin
Copy link
Author

Thanks a lot and totally agree! It worked so nicely before next 14.

@andrewmartin
Copy link
Author

#62008 (comment)

Just so y'all know, this did not work for me. @dmitrc did this work for you with API endpoints (not just the actual page.tsx files?)

@albert-kovalevskij
Copy link

albert-kovalevskij commented Feb 28, 2024

Neither worked for me, tried to follow @dmitrc suggestion but this didn't work for me, that unacceptable, can we fix it, please

P.S. https://nextjs.org/docs/pages/building-your-application/configuring/debugging just being stupid and not reading the docs, actually it worked perfectly by setting "cwd": "${workspaceFolder}/apps/web" for turporepo

@alamit
Copy link

alamit commented Mar 9, 2024

Also struggling with this, can't manage to make server side debugging work

@avianion
Copy link

@andrewmartin still getting this issue when using turbopack on server files with the vscode debugger, did ou fix it

@zanhk
Copy link

zanhk commented Mar 23, 2024

having the same problem as well, does anyone found a solution?

@avianion
Copy link

@x-yuri
might know

@andrewmartin
Copy link
Author

@avianion Yeah, still hitting it unfortunately. I kinda gave up. It's odd because some projects, it works totally fine, like the T3 starter apps. I really can't surmise any meaningful differences in the setup, either.

@avianion
Copy link

The .js files seem to be generated fine, but the corresponding .map files are not found. See here my debugger says

Could not read source map for file:///app/nextjs/.next/server/chunks/e95bda..js: ENOENT: no such file or directory, open 'c:\Users\my_user\Desktop\my_app\nextjs\nextjs.next\server\chunks_e95bda._.js.map'

however the e95bda..js file is itself present, just not the map.

@jensenrrr
Copy link

@albert-kovalevskij Had the solution for me. This only affects those using turborepo.

"P.S. https://nextjs.org/docs/pages/building-your-application/configuring/debugging just being stupid and not reading the docs, actually it worked perfectly by setting "cwd": "${workspaceFolder}/apps/web" for turporepo"

@daguej
Copy link

daguej commented Mar 28, 2024

I've been unable to get next dev to properly start up with debugging enabled. If I use the launch config suggested in the docs, node simply isn't launched with debugging enabled at all. If I try manually, eg:

$ npx --node-options='--inspect' next dev
Debugger listening on ws://127.0.0.1:9229/66073dfe-7554-4b37-8d8d-e4f7dc47789d
For help, see: https://nodejs.org/en/docs/inspector
Starting inspector on 127.0.0.1:9229 failed: address already in use

…this results in the parent next dev process running with debugging, but the worker subprocess tries to debug on the same port, which fails.

I was able to at least get debugging working by starting plain next dev with no debugging enabled, then sending SIGUSR1 to the next-router-worker subprocess spawned by the next dev process. I can then use VS Code to attach to the worker process.

This is a painful workaround; something in Next needs to be fixed to make debugging work properly.

@nassud
Copy link

nassud commented Mar 28, 2024

In all scenarios debugger attaches for me just fine but breakpoints do not bind. I'm guessing is just related to it being unable to find the files. I'm using these configs:

{
      "name": "Next.js: Attach",
      "port": 9230,
      "request": "attach",
      "skipFiles": ["<node_internals>/**"],
      "type": "node",
      "localRoot": "${workspaceFolder}/my-app/src"
    },
    {
      "name": "Next.js: debug server-side",
      "type": "node-terminal",
      "request": "launch",
      "command": "npm run dev",
      "sourceMaps": true,
      "sourceMapPathOverrides": {
        "webpack://_N_E/*": "${webRoot}/*"
      },
      "cwd": "${workspaceFolder}/may-app"
    },

Nothing seems to work

@dexxiez
Copy link

dexxiez commented Apr 4, 2024

Hi all,

I've just been down this rabbit hole and its terrible. By a stroke of luck, I could get it working. I might be repeating what is already said here, just from a different perspective which may help someone.

Full degression, running Next 14.1.4, Node v20.11.0 (LTS) (in WSL - probably irrelevant, so I'll just say Ubuntu). I've tested it in pages and app router.

This is the first time I've actually been able to get a NodeJS application actually to debug, and I can't believe it.

My environment is turborepo (soon to be nx, but thats another story).

I can confirm my relative path to next is apps/web from the root. and in the end, it didn't matter anyway.

My symptoms were a lot the same as the others here, client side is perfect, server side might as well be located in Mars for all I knew. I looked in the NodeJS debug tool and saw that only a subset of my server files were actually being exposed.

image

After actually using my eyes 👀 I saw this.

> NODE_OPTIONS='--inspect' next dev

Debugger listening on ws://127.0.0.1:9229/99f15ee9-6b51-46b4-a070-ccc8bcb60e76
For help, see: https://nodejs.org/en/docs/inspector
Debugger listening on ws://127.0.0.1:9230/42518e76-ee66-44ea-9cb5-a575f1d5fd0c
For help, see: https://nodejs.org/en/docs/inspector
   the --inspect option was detected, the Next.js router server should be inspected at port 9230.

Now call me stupid or ADHD riddled, but I stopped looking normally at the first line mentioning port 9229. It sneaks in 9230 which i found the NodeJS debug tool via Chrome does NOT add by default. At this point I didn't go straight to vscode. I stayed with the Node debugger to do some more testing. I added it in.

image

After adding it in and probably restarting the dev server with the documented normal command via Package JSON script

{
....
"scripts": {
"debug": "NODE_OPTIONS='--inspect' next dev",
}
...

I looked at the sources tab in Node Devtools

image

and it actually had web listed there which is the folder NextJS is in (as im running monorepo). My cardiovascular signs increased. I then added a breakpoint in the Chrome devtools using the file explorer in sources and clicking on a line number in the file previewer.

image

I ran the code, and the breakpoint tripped. I thought I was tripping instead to be honest, this is the closest I've ever gotten.

Only now did I go back to VSCode Debug

I made the new debug file which I've rage deleted many times.

{
    "version": "0.2.0",
    "configurations": [
        {
            "type": "node",
            "request": "attach",
            "name": "Attach to Node",
            "port": 9230,
            "skipFiles": [
                "<node_internals>/**"
            ],
            "cwd": "${workspaceFolder}/apps/web" // I have this because it lives in subfolders, if you're soloing it with no monorepo, you can remove this
        },
        // Below is irrelevant to this post but whatever
        {
            "name": "Next.js: debug client-side",
            "type": "chrome",
            "request": "launch",
            "url": "http://localhost:3000",
            "cwd": "${workspaceFolder}/apps/web",
        },
    ]
}

IMPORTANT SIDE NOTE: This does not start your dev server, it purely connects to what is running, so start your dev server with inspect as per previous Package JSON mention.

The debugger attached but breakpoints are still not red/are hollow with no fill. I tried it anyway, and it worked.

image

Things that did not work for me

  • Preserve symlinks message said to add the argument, I got worse errors so I removed it.
  • Adding a source maps override
  • Adding the outFiles pointing to the .next directory. While it technically did work, vscode would only show the webpack compiled version which is better than nothing, but is yucky none the less.

Things that may be useful troubleshooting

Everyone mentions ${workspaceFolder} in their configurations, but if you are unsure what it is, or you have doubts vscode is selecting the right one, use this echo task to double check it. It's for Linux/WSL so adopt to Windows how you see fit. or ChatGPT it.

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "echo",
            "type": "shell",
            "command": "echo ${workspaceFolder}"
        }
    ]
}

I hope this helps you on your journey of accelerated hair loss.

@DrPye
Copy link

DrPye commented Apr 22, 2024

Hi all, I just wanted to add what I've been using with the app router to get my debugging working as similar to the pages method in the docs. The main thing ive added compared to other suggestions is the ability to debug the full stack in one task.

Tested on Windows with WSL(ubuntu) and using the regular VSCode

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "Next.js: debug server-side",
      "type": "node-terminal",
      "request": "launch",
      "command": "npm run dev --turbo"
    },
    {
      "name": "Next.js: debug client-side",
      "type": "chrome",
      "request": "launch",
      "url": "http://localhost:3000"
    },
    {
      "name": "Next.js: debug full stack",
      "type": "node-terminal",
      "request": "launch",
      "command": "npm run dev --turbo",
      "skipFiles": ["<node_internals>/**"],
      "sourceMaps": true,
      "sourceMapPathOverrides": {
        "/turbopack/[project]/*": "${webRoot}/*"
      },
      "serverReadyAction": {
        "action": "debugWithEdge",
        "killOnServerStop": true,
        "pattern": "- Local:.+(https?://.+)",
        "uriFormat": "%s",
        "webRoot": "${workspaceFolder}"
      }
    }
  ]
}

@togakangaroo
Copy link

Here to say that I finally got this working after a bunch of frustration (using node inspector, I don't tend to use vscode for js debugging usually). The big key was this comment so thank you @dexxiez

Some notes on that

  • Firstly I had to run NODE_OPTIONS='--inspect' next dev
  • Then I pointed my browser to chrome://inspect and opened the inspector
  • I went to the connections tab and added one for localhost:9230
  • I shut down the server and then I shut down the inspector
  • I restarted the server in the same manner
  • Now in chrome://inspect I saw a second option
    screenshot of chrome inspect page showing two options now available
  • I clicked inspect on the second one (the one with start-server) and could actually find next's server in start-server.js. This is promising since it runs in the same process as my code.
  • Now here's the trick, this didn't actually result in my code being visible in node inspector until I actually pointed my browser at the localhost:3000 app and let it load some pages. Only then could I CMD+P and find some of my typescript files. Once I did I could set breakpoints and those breakpoints get hit!

Therefore the trick seems to be to first of all add the appropriate connection, but then also to launch the process, let pages load, and only then try to connect to it via the debugger.

@TechGeorgii
Copy link

TechGeorgii commented Jun 7, 2024

A very simple setup that got it working for me.

In package.json I specified Node inspect port option (default was busy by another project):

"scripts": {
    "dev": "NODE_OPTIONS='--inspect=9237' next dev"
  },

then I run my project npm run dev.

in launch.json :

    {
      "type": "node",
      "request": "attach",
      "name": "Attach to front-end",
      "port": 9238,   // note port is +1 of what's specified in inspect.
      "skipFiles": [
          "<node_internals>/**"
      ],
      "cwd": "${workspaceFolder}/front-end" // I have front-end dir in the workspace, use your setup
    },

And then run attach in VSCode. Breakpoints are working!

Note that ports do not match – I don't know why but node was always starting on port+1 on specified!! (you can see actual port it started in the console).

@0x33dm
Copy link

0x33dm commented Jun 9, 2024

@dmitrc it actually worked with your config. what a ride! took me ages to find your answer. thank you!

@apferrarone
Copy link

apferrarone commented Jun 13, 2024

I tried so many things. I am running a TS monolithic Next.js app, and turns out all I needed to do was turn on auto-attach VSCode feature and launch the app from the VSCode terminal. No configurations or changes, I deleted everything - just run npm run dev from the VSCode terminal w/ auto-attach set to "Always" and it found all the Node processes and the breakpoints are working. To be clear I am just debugging server-side code but this is working for me 🤷‍♂️

@0x33dm
Copy link

0x33dm commented Jun 13, 2024

I tried so many things. I am running a TS monolithic Next.js app and it turns out all I needed to do was turn on the auto-attach the VSCode feature and launch the app from the VSCode terminal. No configurations or changes, I deleted everything - just run npm run dev from the VSCode terminal w/ auto-attach set to "Always" and it found all the Node processes and the breakpoints are working. To be clear I am just debugging server-side code but this is working for me 🤷‍♂️

The same is true for most projects. If you have it to auto-attach "always," there is a good chance it will work when you run it from the terminal. It needs to be "always," apparently, because sometimes a process will launch another and another, and apparently, "always" covers those cases.

In other cases, for instance, in my case, we use turborepo, so we had to do the mapping described by @dmitrc

The browser code is slightly different because the browser needs to be launched with the parameters to allow remote connection on the Chrome inspector.

@zanhk
Copy link

zanhk commented Jul 7, 2024

Does someone have a sample repo where debugging work on windows with turbopack?

@0x33dm
Copy link

0x33dm commented Jul 8, 2024

Does someone have a sample repo where debugging work on windows with turbopack?

try this

.vscode/launch.json

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "Next.js: debug server-side",
      "type": "node-terminal",
      "request": "launch",
      "command": "pnpm run dev",
      "sourceMaps": true,
      "sourceMapPathOverrides": {
        "/turbopack/[project]/*": "${webRoot}/*"
      }
    },
    {
      "name": "Next.js: debug client-side",
      "type": "chrome",
      "request": "launch",
      "url": "http://localhost:3000",
      "webRoot": "${workspaceFolder}",
      "sourceMaps": true,
      "sourceMapPathOverrides": {
        "/turbopack/[project]/*": "${webRoot}/*"
      }
    }
  ]
}

@zanhk
Copy link

zanhk commented Jul 8, 2024

Thanks @0x33dm but unfortunatly with that configuration it does not work

With either "dev": "cross-env NODE_OPTIONS='--inspect' next dev --turbo", or "dev": "cross-env NODE_OPTIONS='--inspect=9230' next dev --turbo", or "dev": "next dev --turbo",

D:\repositories\novafyapp\nova-dogs>bun run dev
$ cross-env NODE_OPTIONS='--inspect=9230' next dev --turbo
Debugger attached.
Starting inspector on 127.0.0.1:9230 failed: address already in use
Starting inspector on 127.0.0.1:9231 failed: address already in use
 ⚠ Port 3000 is in use, trying 3001 instead.
   the --inspect option was detected, the Next.js router server should be inspected at port 9231.
  ▲ Next.js 14.2.4 (turbo)
  - Local:        http://localhost:3001
  - Environments: .env

 ✓ Starting...
 ✓ Ready in 1603ms

It doesn't hit neither the breapoints on server side neither the ones on client side

The only way i got it to work is without using turbo with this config

{
	"version": "0.2.0",
	"configurations": [
		{
			"name": "server",
			"type": "node-terminal",
			"request": "launch",
			"command": "bun run dev"
		},
		{
			"name": "client",
			"type": "chrome",
			"request": "launch",
			"url": "http://localhost:3000"
		},
		{
			"type": "node",
			"request": "attach",
			"name": "attach",
			"port": 9231,
			"skipFiles": ["<node_internals>/**"]
		}
	],
	"compounds": [
		{
			"name": "Debug Server and Client",
			"configurations": ["server", "client", "attach"]
		}
	]
}

"dev": "cross-env NODE_OPTIONS='--inspect=9230' next dev",

But with webpack is slow af

@trozzelle
Copy link

For anyone using > Next.js 14.2, the source map path has changed, per #63740 (comment):

        // Old Turbopack path
        "/turbopack/[project]/*": "${workspaceFolder}/*",
        // New Turbopack path
        "turbopack://[project]/*": "${workspaceFolder}/*"

I'm running the 14.2.3 canary and breakpoint debugging suddenly broke. Changing the sourceMapPathOverrides path like above fixed it for me.

@Rexiao
Copy link

Rexiao commented Oct 8, 2024

For people using

"dependencies": {
    "next": "14.2.14",
}

Here is what works for me:

{
  "version": "0.2.0",
  "compounds": [
    {
      "name": "Debug Start All",
      "configurations": ["Launch Chrome", "Start Nextjs"],
      "stopAll": true
    }
  ],
  "configurations": [
    {
      "type": "chrome",
      "request": "launch",
      "name": "Launch Chrome",
      "url": "http://localhost:3000",
      "webRoot": "${workspaceFolder}",
      "sourceMaps": true,
      "sourceMapPathOverrides": {
        "/turbopack/[project]/*": "${webRoot}/*"
      },
      "presentation": {
        "hidden": true
      }
    },
    {
      "type": "node-terminal",
      "request": "launch",
      "command": "npm run dev",
      "name": "Start Nextjs",
      "sourceMaps": true,
      "sourceMapPathOverrides": {
        "/turbopack/[project]/*": "${webRoot}/*"
      },
      "presentation": {
        "hidden": true
      }
    }
  ]
}

thanks to #62008 (comment)

@sathishkannan162
Copy link

sathishkannan162 commented Oct 17, 2024

{
"version": "0.2.0",
"configurations": [
{
"name": "Next.js: debug server-side",
"type": "node-terminal",
"request": "launch",
"command": "npm run dev",
"sourceMaps": true,
"sourceMapPathOverrides": {
"/turbopack/[project]/": "${webRoot}/"
}
},
{
"name": "Next.js: debug client-side",
"type": "msedge",
"request": "launch",
"url": "http://localhost:3000",
"webRoot": "${workspaceFolder}",
"sourceMaps": true,
"sourceMapPathOverrides": {
"/turbopack/[project]/": "${webRoot}/"
}
}
]
}

This worked for me. It even worked for server actions. I am using turbopack for local development and using Next.js v14.2.14. thanks @dmitrc for the configuration

@matija2209
Copy link

For people using

"dependencies": {
    "next": "14.2.14",
}

Here is what works for me:

{
  "version": "0.2.0",
  "compounds": [
    {
      "name": "Debug Start All",
      "configurations": ["Launch Chrome", "Start Nextjs"],
      "stopAll": true
    }
  ],
  "configurations": [
    {
      "type": "chrome",
      "request": "launch",
      "name": "Launch Chrome",
      "url": "http://localhost:3000",
      "webRoot": "${workspaceFolder}",
      "sourceMaps": true,
      "sourceMapPathOverrides": {
        "/turbopack/[project]/*": "${webRoot}/*"
      },
      "presentation": {
        "hidden": true
      }
    },
    {
      "type": "node-terminal",
      "request": "launch",
      "command": "npm run dev",
      "name": "Start Nextjs",
      "sourceMaps": true,
      "sourceMapPathOverrides": {
        "/turbopack/[project]/*": "${webRoot}/*"
      },
      "presentation": {
        "hidden": true
      }
    }
  ]
}

thanks to #62008 (comment)

Thanks you @Rexiao. This worked for me on 14.2.15 as well. It's the only code that worked.

My package.json script is

"debug": "next dev --turbo --port 4040",

@fcFn
Copy link

fcFn commented Nov 4, 2024

This worked for me after some time (using Nx):

launch.json:

...
{
      "name": "Next.js: debug server-side",
      "type": "node",
      "request": "attach",
      "sourceMaps": true,
      "port": 9230, // Note the port is not the default 9229
      "cwd": "${workspaceFolder}/apps/my-app",
      "sourceMapPathOverrides": {
        "webpack:///./*": "${webRoot}/apps/my-app/*"
 }
 ...

project.json

...
"serve": {
     "command": "cross-env NODE_OPTIONS='--inspect' next dev",
     "defaultConfiguration": "development",
     "production": {},
     "staging": {}
   }
...

Then I would launch npx serve my-app and launch the configuration in VS Code which made the breakpoints hit inside page.tsx. Make sure you're actually opening your page in the browser, otherwise the breakpoints would not hit.

@drollinger
Copy link

drollinger commented Nov 20, 2024

This is what ended up working for me. Using webpack-internal instead. Also, not using the standard port 9229 due to how I have things set up.

"configurations": [
    {
      "name": "Debug Webapp",
      "type": "node",
      "request": "attach",
      "port": 9230,
      "cwd": "${workspaceFolder}/packages/webapp",
      "sourceMaps": true,
      "sourceMapPathOverrides": {
        "webpack-internal:///(rsc)/./*": "${webRoot}/packages/webapp/*"
      }
    },

I found this running the chrome node inspector, hooking it up to the correct port, setting a break point that successfully triggered. Then opening the terminal debugger with node inspect localhost:9230 and triggering the break point and node inspector output the file path of the break point

connecting to localhost:9230 ... ok
break in webpack-internal:///(rsc)/./src/app/api/{..rest of path}

There may be an easier way to find this info but worked for me

@nickwinger
Copy link

Hi all,

I've just been down this rabbit hole and its terrible. By a stroke of luck, I could get it working. I might be repeating what is already said here, just from a different perspective which may help someone.

Full degression, running Next 14.1.4, Node v20.11.0 (LTS) (in WSL - probably irrelevant, so I'll just say Ubuntu). I've tested it in pages and app router.

This is the first time I've actually been able to get a NodeJS application actually to debug, and I can't believe it.

My environment is turborepo (soon to be nx, but thats another story).

I can confirm my relative path to next is apps/web from the root. and in the end, it didn't matter anyway.

My symptoms were a lot the same as the others here, client side is perfect, server side might as well be located in Mars for all I knew. I looked in the NodeJS debug tool and saw that only a subset of my server files were actually being exposed.

image

After actually using my eyes 👀 I saw this.

> NODE_OPTIONS='--inspect' next dev

Debugger listening on ws://127.0.0.1:9229/99f15ee9-6b51-46b4-a070-ccc8bcb60e76
For help, see: https://nodejs.org/en/docs/inspector
Debugger listening on ws://127.0.0.1:9230/42518e76-ee66-44ea-9cb5-a575f1d5fd0c
For help, see: https://nodejs.org/en/docs/inspector
   the --inspect option was detected, the Next.js router server should be inspected at port 9230.

Now call me stupid or ADHD riddled, but I stopped looking normally at the first line mentioning port 9229. It sneaks in 9230 which i found the NodeJS debug tool via Chrome does NOT add by default. At this point I didn't go straight to vscode. I stayed with the Node debugger to do some more testing. I added it in.

image

After adding it in and probably restarting the dev server with the documented normal command via Package JSON script

{
....
"scripts": {
"debug": "NODE_OPTIONS='--inspect' next dev",
}
...

I looked at the sources tab in Node Devtools

image

and it actually had web listed there which is the folder NextJS is in (as im running monorepo). My cardiovascular signs increased. I then added a breakpoint in the Chrome devtools using the file explorer in sources and clicking on a line number in the file previewer.

image

I ran the code, and the breakpoint tripped. I thought I was tripping instead to be honest, this is the closest I've ever gotten.

Only now did I go back to VSCode Debug

I made the new debug file which I've rage deleted many times.

{
    "version": "0.2.0",
    "configurations": [
        {
            "type": "node",
            "request": "attach",
            "name": "Attach to Node",
            "port": 9230,
            "skipFiles": [
                "<node_internals>/**"
            ],
            "cwd": "${workspaceFolder}/apps/web" // I have this because it lives in subfolders, if you're soloing it with no monorepo, you can remove this
        },
        // Below is irrelevant to this post but whatever
        {
            "name": "Next.js: debug client-side",
            "type": "chrome",
            "request": "launch",
            "url": "http://localhost:3000",
            "cwd": "${workspaceFolder}/apps/web",
        },
    ]
}

IMPORTANT SIDE NOTE: This does not start your dev server, it purely connects to what is running, so start your dev server with inspect as per previous Package JSON mention.

The debugger attached but breakpoints are still not red/are hollow with no fill. I tried it anyway, and it worked.

image

Things that did not work for me

  • Preserve symlinks message said to add the argument, I got worse errors so I removed it.
  • Adding a source maps override
  • Adding the outFiles pointing to the .next directory. While it technically did work, vscode would only show the webpack compiled version which is better than nothing, but is yucky none the less.

Things that may be useful troubleshooting

Everyone mentions ${workspaceFolder} in their configurations, but if you are unsure what it is, or you have doubts vscode is selecting the right one, use this echo task to double check it. It's for Linux/WSL so adopt to Windows how you see fit. or ChatGPT it.

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "echo",
            "type": "shell",
            "command": "echo ${workspaceFolder}"
        }
    ]
}

I hope this helps you on your journey of accelerated hair loss.

Thank you, this is also working on my side now. I also had the feeling that there are to many processes started for debugging on different ports, however just attaching the VSCode to the 9230 running process is the easiest way.
So on my side i started "turbo dev" in an external command line and then attached VSCode like you said. Also the breakpoints are red and working on server side !

@massimokris
Copy link

@tianzhich

Have you guys solved this in server-sider api debugging?

this set up it's working for me. the only thing i didn't manage to make it open the terminal in the editor code, so i have the debugger, but not the terminal

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "Next.js: debug API (server-side)",
      "type": "node",
      "request": "launch",
      "program": "${workspaceFolder}/node_modules/next/dist/bin/next",
      "args": [
        "dev"
      ],
      "cwd": "${workspaceFolder}",
      "autoAttachChildProcesses": true,
      "skipFiles": [
        "<node_internals>/**"
      ],
      "serverReadyAction": {
        "pattern": "started server on .+? \\(http://.+?\\)",
        "uriFormat": "%s",
        "action": "openExternally"
      }
    }
  ]
}

@nameofname
Copy link

Thanks @TechGeorgii for your post, I was bashing my head against a wall b/c the attach to port option used to work for me then stopped after some upgrades. The trick for me was your comment :

"port": 9238, // note port is +1 of what's specified in inspect.

I have the following in package.json :
--inspect=9231

I switched my attach to port config to use 9232 and it worked! Development accelerated! Debug config for those who want to try it out :

            {
                "type": "node",
                "request": "attach",
                "name": "Node - Attach to Port",
                "port": 9232,
                "skipFiles": [
                    "<node_internals>/**"
                ]
            },

@Plazmius
Copy link

Plazmius commented Jan 5, 2025

For anyone struggling to debug server-side - I spend a few hours trying various configs, and nothing worked, before arriving at something that actually worked and was more or less convenient:

{
    "version": "0.2.0",
    "configurations": [
      {
        "name": "Next.js: launch server-side",
        "type": "node-terminal",
        "request": "launch",
        "command": "npm run dev"
      },
      {
        "name": "Next.js: debug server-side",
        "port": 9230,
        "request": "attach",
        "skipFiles": [
          "<node_internals>/**"
        ],
        "type": "node"
      },
      {
        "type": "chrome",
        "request": "launch",
        "name": "Next.js: debug client-side",
        "url": "http://localhost:3000",
        "webRoot": "${workspaceFolder}"
      }
    ],
    "compounds": [
      {
        "name": "Next.js: debug full stack",
        "configurations": ["Next.js: launch server-side", "Next.js: debug client-side", "Next.js: debug server-side"],
        "stopAll": true
      }
    ]
  }

It's a shame that the example from the docs did not work out of the box.

P.S. Config is inspired by this - https://gist.github.com/tomdohnal/9a251c4e25136213509e5b1293ffc641, many thanks to the author. I just added launching the app to the compound.

@beerent
Copy link

beerent commented Jan 18, 2025

{
"type": "node",
"request": "attach",
"name": "Attach to Node",
"port": 9230,
"skipFiles": [
"<node_internals>/**"
],
"cwd": "${workspaceFolder}/apps/web" // I have this because it lives in subfolders, if you're soloing it with no monorepo, you can remove this
},

this breakdown is absolutely insane. everything was identical on my end, down to the emotional rollercoaster. Thank you!

@latinrev
Copy link

latinrev commented Jan 29, 2025

thanks @nameofname

Serverside debugging with Windows turbopack config,
"dev": "cross-env NODE_OPTIONS='--inspect=9229' next dev --turbopack",

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "Next.js: launch server-side",
      "type": "node-terminal",
      "request": "launch",
      "command": "pnpm dev"
    },
    {
      "name": "Next.js: debug server-side",
      "port": 9230,
      "request": "attach",
      "skipFiles": ["<node_internals>/**"],
      "type": "node"
    },
    {
      "type": "chrome",
      "request": "launch",
      "name": "Next.js: debug client-side",
      "url": "http://localhost:3000",
      "webRoot": "${workspaceFolder}"
    }
  ],
  "compounds": [
    {
      "name": "Next.js: debug full stack",
      "configurations": ["Next.js: launch server-side", "Next.js: debug client-side", "Next.js: debug server-side"],
      "stopAll": true
    }
  ]
}

PS: Stopped working

@mdmahendri
Copy link

@tianzhich

Have you guys solved this in server-sider api debugging?

this set up it's working for me. the only thing i didn't manage to make it open the terminal in the editor code, so i have the debugger, but not the terminal

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "Next.js: debug API (server-side)",
      "type": "node",
      "request": "launch",
      "program": "${workspaceFolder}/node_modules/next/dist/bin/next",
      "args": [
        "dev"
      ],
      "cwd": "${workspaceFolder}",
      "autoAttachChildProcesses": true,
      "skipFiles": [
        "<node_internals>/**"
      ],
      "serverReadyAction": {
        "pattern": "started server on .+? \\(http://.+?\\)",
        "uriFormat": "%s",
        "action": "openExternally"
      }
    }
  ]
}

thank you, i am using nextjs 15 and encounter issue when debugging server API, this code works for me! and yest the terminal does not open

@nomtats
Copy link

nomtats commented Feb 5, 2025

I wrestled with it for a long time but couldn't get it work no matter what sourceMapOverride I set.

I solved it eventually by calling npx next dev instead of pnpm dev.
(Note that npx next dev --turbopack didn't work.)

    {
      "name": "Next.js: Server",
      "type": "node-terminal",
      "request": "launch",
      "cwd": "${workspaceFolder}/apps/web",
      "command": "npx next dev",
      "env": {
        "NODE_OPTIONS": "--inspect"
      },
      "skipFiles": [
        "<node_internals>/**"
      ],
    },

Another thing that help was to add a debugger in the source code to force the debugger to break.

function() {
  // ....
  debugger;
  // ...
}

It seems the debugger confuses the code path thinking the source code is at apps/web/apps/web/... as opposed to just apps/web/.
I don't know why and couldn't make it work with sourceMapOverride.

@uap-dev
Copy link

uap-dev commented Feb 6, 2025

I wrestled with it for a long time but couldn't get it work no matter what sourceMapOverride I set.

I solved it eventually by calling npx next dev instead of pnpm dev. (Note that npx next dev --turbopack didn't work.)

{
  "name": "Next.js: Server",
  "type": "node-terminal",
  "request": "launch",
  "cwd": "${workspaceFolder}/apps/web",
  "command": "npx next dev",
  "env": {
    "NODE_OPTIONS": "--inspect"
  },
  "skipFiles": [
    "<node_internals>/**"
  ],
},

Another thing that help was to add a debugger in the source code to force the debugger to break.

function() {
// ....
debugger;
// ...
}
It seems the debugger confuses the code path thinking the source code is at apps/web/apps/web/... as opposed to just apps/web/. I don't know why and couldn't make it work with sourceMapOverride.

{
      "name": "Next.js: debug server: launch",
      "type": "node",
      "request": "launch",
      "cwd": "${workspaceFolder}/packages/web-next",
      "program": "${workspaceFolder}/packages/web-next/node_modules/next/dist/bin/next",
      // "args": ["dev", "--turbopack"],
      "args": ["dev"],
      "runtimeArgs": ["--inspect"],
      "skipFiles": [
        "<node_internals>/**",
        "${workspaceFolder}/node_modules/**",
        "${workspaceFolder}/packages/web-next/node_modules/**"
      ]
    }

Had a similar config and running into the same issue where the turbopack flag prevents certain breakpoints from being reachable. My middleware breakpoints can be hit with ["dev", "--turbopack"], but not my server component code. That only works when I run dev without the turbopack flag, but the dev server is so slow its almost unusable without turbopack.

@Danzanus
Copy link

Danzanus commented Feb 8, 2025

{
            "name": "Next.js: debug full stack",
            "program": "${workspaceFolder}/my-app/node_modules/next/dist/bin/next",
            "args": [
                "dev",
                "--turbopack",
            ],
            "runtimeArgs": [
                "--inspect"
            ],
            "skipFiles": [
                "<node_internals>/**",
            ],         
            "type": "node",
            "request": "launch",
            "sourceMaps": true,
            "serverReadyAction": {
                "killOnServerStop": true,
                "webRoot": "${workspaceFolder}/my-app",
                "pattern": "- Local:.+(https?://.+)",
                "uriFormat": "%s",
                "action": "debugWithChrome"
            },
            "sourceMapPathOverrides": {
                "file://C%3A/WORK/VSCODE/PROJECTS/my-app/*": "${workspaceFolder}/my-app/*",
                "start-server.js/*": "${workspaceFolder}/*",
            },
            "cwd": "${workspaceFolder}/my-app",
        }

After try and errors this works for me with --turbopack. Im not actually sure why and how. This is my first attempt to debug nextjs with actual debugger instead of console.log.

"file://C%3A/WORK/VSCODE/PROJECTS/my-app/": "${workspaceFolder}/my-app/"

I figured out that when i run with turbo, i get encoded C%3A instead of C: in chrome dev tools -> sources.
So this line fixed client side debugging. But server side didnt work.

Then i check vscode "Loaded Scripts" tab.
Notice that files i need to debug stored in - Next.js: debug full stack -> start-server.js -> my-app -> src -> app
so i put

"start-server.js/": "${workspaceFolder}/"

and it works for me.

Client side is related to #73384 i think.
And server side is more like #62008, but instead of turbopack i get start-server.js

So check your "dev-tools -> sources" and vscode Loaded Scripts for client and server side files and adjust path in overrides accordingly.

@anthonyalayo
Copy link

anthonyalayo commented Feb 12, 2025

This issue is looking like my discussion here as well as my issue that I attempted to open here.

After PR #71940, merged in v15.0.3-canary.4, breakpoints no longer line up on server side code. The EvalSourceMapDevToolPlugin got changed from using a relative path to an absolute path. cc @icyJoseph

From what it looks like on this chat, people are working around the bug by trying to patch their webpack configurations.

@sevillal
Copy link

@tianzhich

Have you guys solved this in server-sider api debugging?

this set up it's working for me. the only thing i didn't manage to make it open the terminal in the editor code, so i have the debugger, but not the terminal

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "Next.js: debug API (server-side)",
      "type": "node",
      "request": "launch",
      "program": "${workspaceFolder}/node_modules/next/dist/bin/next",
      "args": [
        "dev"
      ],
      "cwd": "${workspaceFolder}",
      "autoAttachChildProcesses": true,
      "skipFiles": [
        "<node_internals>/**"
      ],
      "serverReadyAction": {
        "pattern": "started server on .+? \\(http://.+?\\)",
        "uriFormat": "%s",
        "action": "openExternally"
      }
    }
  ]
}

thank you, i am using nextjs 15 and encounter issue when debugging server API, this code works for me! and yest the terminal does not open

This worked for me to open the terminal:

{
        "name": "Next.js: debug API (server-side)",
        "type": "node-terminal",
        "request": "launch",
        "command": "node ${workspaceFolder}/node_modules/next/dist/bin/next dev",
        "cwd": "${workspaceFolder}",
        "autoAttachChildProcesses": true,
        "skipFiles": [
          "<node_internals>/**"
        ],
        "serverReadyAction": {
          "pattern": "started server on .+? \\(http://.+?\\)",
          "uriFormat": "%s",
          "action": "openExternally"
        }
      }

@roicahana
Copy link

roicahana commented Mar 1, 2025

This is how I got it working for Next.js 15.2:

{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Next.js: Server",
"type": "node-terminal",
"request": "launch",
"command": "npx next dev"
},
{
"name": "Next.js: Client",
"type": "chrome",
"request": "launch",
"url": "http://localhost:3000"
}
],
"compounds": [
{
"name": "Debug Next.js (Client + Server)",
"configurations": ["Next.js: Client", "Next.js: Server"]
}
]
}

@fcFn
Copy link

fcFn commented Mar 17, 2025

On another computer, a Mac, my previous configuration for an nx mono repo app didn't work, so this time I had to use this for the app to pick up the correct paths to the files on the disk:

{
      "name": "Next.js: debug server-side",
      "type": "node",
      "port": 9230,
      "request": "attach",
      "cwd": "${workspaceFolder}",
      "sourceMapPathOverrides": {
        "webpack-internal:///(rsc)/*": "apps/my-app/*"
      }
}

Strangely the same sourceMapPathOverrides doesn't work with node-terminal and command...

@anthonyalayo
Copy link

This issue is looking like my discussion here as well as my issue that I attempted to open here.

After PR #71940, merged in v15.0.3-canary.4, breakpoints no longer line up on server side code. The EvalSourceMapDevToolPlugin got changed from using a relative path to an absolute path. cc @icyJoseph

From what it looks like on this chat, people are working around the bug by trying to patch their webpack configurations.

Is there a workaround for IDE's outside of VS Code? I am still pinning to this older version because of the bug. @icyJoseph ?

@anthonyalayo
Copy link

@icyJoseph with the big security vulnerability we are now forced to chose between secure and debuggable. Is it possible to revert #71940 so we have something to work with?

@cvbge
Copy link

cvbge commented Apr 6, 2025

Still does not work with Turbopack in latest versions (on Windows):

$ npx create-next-app@latest --version
15.2.4
$ code --version
1.99.0
4437686ffebaf200fa4a6e6e67f735f3edf24ada
x64

With default settings, web app is using Turbopack:

$ npx create-next-app@latest --reset
$ npx create-next-app@latest

Server-side debugging with https://nextjs.org/docs/app/building-your-application/configuring/debugging settings do not work - breakpoints are not active.
Setting "cwd": "${workspaceFolder}/apps/web" does not work because this folder does not exist, tried other paths, didn't work.

If Turbopack is disabled during create-next-app then those debugging settings from docs work out of the box.

@Kamitenshi
Copy link
Contributor

Thanks to @nomtats I was able to debug effectively 🎉

What worked for me was to put directly into my code the debugger directive:

Image

If you're wondering here's my launch.json config:

    {
      "name": "Next.js: debug server-side",
      "type": "node-terminal",
      "request": "launch",
      "command": "npm run dev",
      "cwd": "${workspaceFolder}/webapp",
    }

(Using cwd since I'm in a monorepo)

And here's my package.json script section:

  "scripts": {
    "dev": "next dev --turbopack"
  }

As you can see, I'm still using TurboPack which allows to compile faster.

I hope this helps some of you to debug until this issue is fixed!

@Kamitenshi
Copy link
Contributor

Update: I was able to make the fullstack debugger work 🎉

BUT the UX has confused me.

When setting the breakpoints and launching the debugger they look unbound.
But then, they always get hit and I can debug properly.

On my side, client code, server components and server actions could be debugged properly.

Here's my configuration:

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "Next.js: debug full stack",
      "type": "node",
      "request": "launch",
      "program": "${workspaceFolder}/webapp/node_modules/next/dist/bin/next",
      "runtimeArgs": ["--inspect"],
      "skipFiles": ["<node_internals>/**"],
      "serverReadyAction": {
        "action": "debugWithChrome",
        "killOnServerStop": true,
        "pattern": "- Local:.+(https?://.+)",
        "uriFormat": "%s",
        "webRoot": "${workspaceFolder}"
      },
      "cwd": "${workspaceFolder}/webapp"
    }
  ]
}

@neilenns
Copy link

neilenns commented Apr 27, 2025

I'm also unable to get this to work, and I've tried all sorts of variations that others have suggested with no luck. Here's my launch.json config:

    {
      "name": "Website",
      "type": "node",
      "request": "launch",
      "runtimeExecutable": "pnpm",
      "runtimeArgs": ["exec", "turbo", "run", "web#dev"],
      "console": "integratedTerminal",
      "internalConsoleOptions": "neverOpen",
      "restart": false,
      "serverReadyAction": {
        "pattern": "- Local:.+(https?://.+)",
        "uriFormat": "%s",
        "action": "debugWithEdge",
        "killOnServerStop": true,
        "webRoot": "${workspaceFolder}/apps/web/src"
      }
    },

I'm using turborepo and my monorepo is set up as one would expect: apps/web is the folder for the nextjs site, and apps/web/src has the source code in it.

@keeehlan
Copy link

The year is 2025. Full-stack web developers are still being forced to write debugger statements in their JS/TS code. Any attempt at using a breakpoint in their IDE instantly afflicts them with schizophrenic hallucinations. The curse is spreading. There's no hope in sight.

I'm definitely surprised to see this is an active issue. One of the reasons I chose this framework and toolset was to ensure a painless development experience, but I'm losing valuable time fighting through seemingly simple issues.

There are a lot of suggestions above. Has there been any definitive progress towards a solution, or is there at least a workaround that doesn't send us back in time?

@neilenns
Copy link

neilenns commented Apr 29, 2025

Has there been any definitive progress towards a solution, or is there at least a workaround that doesn't send us back in time?

I've spent two days trying to come up with a monorepo setup that supports:

  • hitting breakpoints in VSCode in monorepo packages
  • hot reloading
  • no red squiggles in VSCode

For a monorepo using Typescript, nextjs for the app, expressjs for the API server, and two basic packages. I swear this must be the most common configuration for people writing a frontend and backend in Typescript.

And yet. AND YET.

I got nowhere.

Got so annoyed I even went on a Twitter rant about it. You can see my continuing descent into madness in my personal open issue on it.

@VenyaBrodetskiy
Copy link

Hi, guys! any updates here?

for today, using setup from official documentation https://nextjs.org/docs/pages/guides/debugging:
v. 14.2.28 debugger is attached, breakpoints inside getStaticProps are hit
v. 15.3.1 debugger is attached, breakpoints are unbound

@ingridskard
Copy link

Update: I was able to make the fullstack debugger work 🎉

BUT the UX has confused me.

When setting the breakpoints and launching the debugger they look unbound. But then, they always get hit and I can debug properly.

On my side, client code, server components and server actions could be debugged properly.

Here's my configuration:

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "Next.js: debug full stack",
      "type": "node",
      "request": "launch",
      "program": "${workspaceFolder}/webapp/node_modules/next/dist/bin/next",
      "runtimeArgs": ["--inspect"],
      "skipFiles": ["<node_internals>/**"],
      "serverReadyAction": {
        "action": "debugWithChrome",
        "killOnServerStop": true,
        "pattern": "- Local:.+(https?://.+)",
        "uriFormat": "%s",
        "webRoot": "${workspaceFolder}"
      },
      "cwd": "${workspaceFolder}/webapp"
    }
  ]
}

Thanks, tis worked for me 🥳 Not in a monorepo, here's my config:

{
  "name": "Next.js: debug full stack",
  "type": "node",
  "request": "launch",
  "program": "${workspaceFolder}/node_modules/next/dist/bin/next",
  "runtimeArgs": ["--inspect"],
  "skipFiles": ["<node_internals>/**"],
  "serverReadyAction": {
    "action": "debugWithChrome",
    "killOnServerStop": true,
    "pattern": "- Local:.+(https?://.+)",
    "uriFormat": "%s",
    "webRoot": "${workspaceFolder}"
  }
}

Also not seeing my server-side breakpoints light up, but they get hit and I can debug normally.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Issue was opened via the bug report template.
Projects
None yet
Development

No branches or pull requests