belgattitude / nextjs-monorepo-example

Collection of monorepo tips & tricks
MIT License
1.61k stars 212 forks source link

Debug Nexjs app in monorepo with "NODE_OPTIONS='inspect'" #3948

Closed andresgutgon closed 1 year ago

andresgutgon commented 1 year ago

What?

Hi, nice to meet you. I've been looking for a solution for this hours and I don't find anything. So first of all sorry for creating an issue in this repo 🙏. But I think this can be really helpful for a lot of people. Debugging Node is not always easy

The problem. Debug node server NextJS app

I'm following their oficial docs: https://nextjs.org/docs/pages/building-your-application/configuring/debugging#server-side-code

But these docs are for a flat next app not for an app living in a monorepo like this so maybe someone already did this.

In my case this is my setup

I'm trying this

turbo run dev:debug

And i have a nextjs app called apps/web/package.json

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

I see the debugger session:

web:dev:debug: > NODE_OPTIONS='--inspect' next dev
web:dev:debug:
web:dev:debug: Debugger listening on ws://127.0.0.1:9229/63260d0c-65a0-4fa7-8285-e42487920401
web:dev:debug: For help, see: https://nodejs.org/en/docs/inspector
web:dev:debug: - ready started server on 0.0.0.0:3000, url: http://localhost:3000

And going to the app on http://localhost:3000 I see Node debugger session

image

But if I open it any file of my app is there. Only react

image

I'm out of idea 😓

belgattitude commented 1 year ago

I'll have a look next week. It's totally new for me but nice you've shared it. Don't get too frustrated though, perfectly normal sometimes 😀

belgattitude commented 1 year ago

BTW really feel free to make a small pr with the dev:debug script on this repo. I'll merge and try.

andresgutgon commented 1 year ago

Thanks! I'm trying to take a scientific aproach here 🤓 starting by standalone nextjs with javascript and go up to full monorepo pnpm typescript

https://github.com/andresgutgon/nextjs-debugging

So far I'm in the most simple case following their docs and I'm not understanding how debugging works in node. I did manage to debug a simple express one file server tho. So much be something related with nextjs and sourcemaps.

belgattitude commented 1 year ago

Nice. Have you tried it with a getserversideprops route ?

andresgutgon commented 1 year ago

With default pages/api/hello which is ultra simple. The problem is that Chrome Dev Tools don't even see that code so I can't put a debugger

belgattitude commented 1 year ago

mmm. I saw that you use turbo to run the command, might be that too ? (ie https://github.com/vercel/turbo/blob/main/packages/turbo/install.js#L190)

andresgutgon commented 1 year ago

Yes, I use turbo in my app but in the playground i use plain nextjs app with npm. I want to start as simple as possible and understand what's happening

belgattitude commented 1 year ago

Let me know when you find out... super interested as well. And thanks for your work

andresgutgon commented 1 year ago

Ok I think I found the right issue I'm having I'm able to stop the debugger 🎉

image

The fix is in this PR which is still pending https://github.com/vercel/next.js/pull/51467

I wanted to try so I forked NextJS repo and copied these changes and looks it working

belgattitude commented 1 year ago

Thanks for the tip.