I encountered a similar issue, but managed to resolve it through a two-step process:
To start, I included a line in my VSCode settings.json file:
"debug.javascript.usePreview": false
Additionally, I made sure not to use
NODE_OPTIONS="--inspect"
After making these adjustments, you may still encounter unbound breakpoints. However, by running the application and setting the breakpoint again, it should turn red and function properly.
Update
In some cases, I faced difficulty setting breakpoints on API calls. After spending some time investigating the root cause, I discovered the problem:
When running a Next.js app, Node first executes the next script, which then spawns a child process containing your code. Next.js automatically sets NODE_OPTIONS=--inspect
when in development mode, but with a different port number that changes dynamically. The CRUCIAL POINT is this: The debugging port numbers for the Next script and the child process differ, resulting in occasional inability to set breakpoints.
Here are a few scenarios:
- If you manually start the server in the VSCODE terminal by entering "npm run dev," VSCODE will easily locate the debugging ports and everything should work smoothly.
- If you launch the server outside of VSCODE from a terminal and then utilize
attach
, VSCODE will only attach to one port, typically linked to the Nextjs script. This explains why you might struggle to set breakpoints in your own code.
- Using the
launch
method to initiate the server in launch.json leads to a similar outcome as No.2 - breakpoints cannot be set effectively.
A simple solution exists: Either commence the server from the VSCODE internal terminal or include the following line in your launch.json
:
"autoAttachChildProcesses": true,
This configuration enables smooth debugging operations by pressing F5.
{
"name": "Next.js: debug full stack",
"type": "node-terminal",
"request": "launch",
"command": "npm run dev",
"serverReadyAction": {
"pattern": "started server on .+, url: (https?://.+)",
"uriFormat": "%s",
"action": "debugWithChrome"
},
"autoAttachChildProcesses": true,
}
No need to add NODE_OPTION='--inspect'
or modify
"debug.javascript.usePreview": false
. You're all set!