39

I'm trying to use the WebStorm IDE debugger to debug a NextJS React app. I tried to use a JavaScript configuration, but that didn't seem to work -- neither when I used a Node config.

What's the correct procedure for debugging NextJS React apps with WebStorm?

6 Answers 6

48

The following steps work for me:

  • start the app with next (npm run dev or whatever your start script looks like)
  • add breakpoints, create JavaScript Debug run configuration, specify http://localhost:3000 URL
  • debug

If you like to debug the code that is executed on the server side, I'd suggest using the Node.js run configuration with node_modules\next\dist\bin\next specified as Javascript file:

enter image description here

You can then debug both Node.js and Javascript Debug run configurations to get both server-side and client-side code debugged.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks, that seems to work with simple projects, but in the project I'm working on the "pages" directory is not in the project root, but some levels down, '/src/common/pages' and when I launch the debugger I get the error "Debugger attached. > Couldn't find a pages directory. Please create one under the project root". I am trying at the moment to debug a node module at the moment, so I don't want to set the project root to the 'common' directory...
In my case, there was no .next/dist/bin/next file, regardless of production or dev deploy. Clicking the debug mode on package.json's next script seemed to work for the server side debug.
I confirmed that next command in package.json scripts just starts debugging with Node 12.14.1, while v13.3.0 couldn't connect the debugger.
I can confirm that this works on NextJs version 14.1.0. Thanks for help!
43

I found that clicking the play button next to dev in package.json and selecting the debug option worked for debugging my API served by Next.js.

enter image description here

This automatically creates the run configuration. Breakpoints are flakey though, I think because I'm using TypeScript. I need to edit the file after adding the breakpoint for them to work.

see official docs for more details: https://www.jetbrains.com/help/webstorm/running-and-debugging-node-js.html

6 Comments

It doesn't stop at the breakpoints me even if I edit the file. Although I am using PyCharm pro version, not WebStorm.
same for me using WebStorm, some of the breakpoints seems to be working, but not all of them.
If only some of your breakpoints are working, it could be that you're only capturing server-side or client-side breakpoints. Remember NextJS can run the same code server-side (SSR) or client-side
@Nils Works for me on the server but don't detect breakpoints in Browser! how can fix it?
I think we have to add the breakpoint on the generated .js file... it seems to work there...
|
8

for the Frontend of next.js:

Start debugging your app right from the Run tool window below ↓. Just hold Ctrl+Shift / ⌘⇧ and click the URL address where the application is running. WebStorm launches the auto generated Debug Application run/debug configuration, the browser opens at http://localhost:3000/, and the Debug tool window appears showing you the call stack and variables.

enter image description here

https://blog.jetbrains.com/webstorm/2017/01/debugging-react-apps/#launch_a_debugging_session

3 Comments

he say Next js You say Create React App?!! do not useful for Next js.
@MohmmadEbrahimiAval No, I'm actually demonstrating how to do this in a NEXT.JS app. and since it's identical to react I also provided further instructions...
This is the only thing that worked for me.
1

In 2025, you need to disable the --turbopack option run the dev run configuration for debugging to work in webstorm.

I made a dev-no-turbo run configuration in package.json for use in debugging:

"dev": "next dev --turbopack", "dev-no-turbo": "next dev", 

2 Comments

Excluding --turbopack worked for making execution stop at breakpoints for me!! But I also have a visual glitch: it's not the line with breakpoint that gets highlighted with blue color – but rather another line that is 7 lines above the breakpoint. 🤷‍♂️🙂 Did you have similar experience?
Hmm, I did not!
0

I tried using both WebStorm and Rider, and in both of these IDEs, breakpoints randomly stop being hit after some time.

As a workaround, I just stop and rerun the process, which makes breakpoints hit again.

If there is a better way to cope with this weird problem, I'd love to hear it in the comments.

Comments

0

For the front end, try adding a "Launch Web Browser" action to your dev configuration's "Before Launch" list, and enable Javascript debugger:

enter image description here

and then click the debug icon, here:

enter image description here

This is the only way I've managed to get it working.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.