Hi,
Would like to request for documentation using breakpoint/debugging.
Seems some of the recommended ways on the internets are out of date now (on Spectrum / SO, etc..).
Some documentation from Zeit would be awesome..
Just going to add a couple of notes from my experience as of Next.js 9.0.3.
โ
This config in .vscode/launch.json works for me:
{
"type": "node",
"request": "launch",
"name": "Next.js server-side",
"runtimeExecutable": "node",
"runtimeArgs": ["--inspect", "node_modules/.bin/next", "dev"],
"port": 9229
}
I can then do this in VSCode:
pages/_app.tsx, both in getInitialProps and in render.pages/some-other-page.tsx, both in getInitialProps and in render.Note how our code is in TypeScript but I didn't need to add -r ts-node/register or similar.
If I had the next binary installed globally, this should also work (note the NODE_OPTIONS approach):
{
"type": "node",
"request": "launch",
"name": "Global next binary",
"runtimeExecutable": "next",
"runtimeArgs": ["dev"],
"env": {
"NODE_OPTIONS": "--inspect"
},
"port": 9229
},
But I haven't tried as we have Next.js as a devDependency.
๐ซ A method that doesn't work for me (but I generally like it) is having something like this in package.json:
{
"scripts": {
"dev": "next dev",
"dev:debug": "node --inspect node_modules/.bin/next dev"
}
}
Then run yarn dev:debug it iTerm or similar and launch a "Node: Attach" config:
{
"type": "node",
"request": "attach",
"name": "Node attach",
"port": 9229
}
This works for our plain Node.js projects but in a Next.js app, even though VSCode seemingly connects to the debugger (the status bar goes orange, the Debug Console mirrors console output, etc.), my breakpoints are not hit. I'm almost sure that it has something to do with the process being started through yarn and not directly. If anyone knows how to fix this, I'd be interested.
โ A workaround for this is, of course, to run the command directly:
node --inspect node_modules/.bin/next dev
(or --inspect-brk).
โ
If I don't need the familiarity and comfort of VSCode, by far the easiest is to just debug in Chrome directly. In DevTools, I go to Sources panel, press the familiar Cmd+P, browse to a file like webpack://./pages/_app.tsx (yes, it's TypeScript directly) and set breakpoints there.
โ Debugging via VSCode works as well but there's a bit of a setup. After installing this VSCode extension, the most annoying part is starting Chrome with remote debugging on, which on a Mac is done from the command line:
/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome --remote-debugging-port=9222
For this to work, Chrome must be shut down entirely or otherwise, the existing instance will be used, without the debugger. (I use Chrome Canary for development so that I don't need to mess with my main Chrome browser but still, it's inconvenient.)
When the setup is done, this launch config is enough to connect VSCode to Chrome:
{
"type": "chrome",
"request": "attach",
"name": "Chrome attach",
"port": 9222
}
It's also possible to launch Chrome directly:
{
"type": "chrome",
"request": "launch",
"name": "Launch Chrome",
"url": "http://localhost:3000",
"webRoot": "${workspaceFolder}"
}
It's possible to run both server-side and client-side debugging in VSCode using a compound configuration, for example:
{
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Next.js server-side",
// etc.
},
{
"type": "chrome",
"request": "attach",
"name": "Next.js client-side",
// etc.
}
],
"compounds": [
{
"name": "Next.js full-stack",
"configurations": ["Next.js server-side", "Next.js client-side"]
}
]
}
With Next.js 9 dynamic routing, we no longer have a reason to use a custom server but previously, we had a config like this:
{
"type": "node",
"request": "launch",
"name": "Next.js custom server",
"runtimeArgs": ["-r", "ts-node/register", "--inspect", "server/server.ts"],
"env": {
"TS_NODE_PROJECT": "tsconfig.server.json"
},
"port": 9229,
"stopOnEntry": false
}
We also had a dev:debug script in package.json that looked like this:
{
"scripts": {
"dev:debug": "NODE_ENV=development TS_NODE_PROJECT=tsconfig.server.json node -r ts-node/register --inspect server/server.ts",
}
}
I'm pretty sure it worked fine with the _Node attach_ configuration. Overall, custom server was not a problem. โ
pages/post/[id].js: #8295Thanks for the guide. the server-side debugging seems working.
However the client-side debugging is not.
I tried the above configuration (both attach and launch mode) but breakpoints are still greyed out.
Anyone else facing the same issue as me?
@lavaxun Does it work when you debug directly in Chrome?
Hey there, I just used Chrome DevTools to debug a Next.js app, here's how: https://github.com/zeit/next.js/pull/10807m, let me know!
I think clearer documentation for server-side debugging would be useful on https://nextjs.org/docs/api-reference/cli.
Its current suggestion is to run NODE_OPTIONS='--inspect' next. That may work on as a standalone command, however if you bake that into package.json and also use typescript, then you get the dreaded "address already in use" bug (https://github.com/zeit/next.js/issues/9027). And the solution is not entirely obvious.
I'd like to suggest the documentation provide a boilerplate package.json with server-side debugging included. This works for me:
{
"scripts": {
"debug": "node --inspect node_modules/.bin/next dev",
}
}
Most helpful comment
Just going to add a couple of notes from my experience as of Next.js 9.0.3.
Server-side
โ This config in
.vscode/launch.jsonworks for me:I can then do this in VSCode:
pages/_app.tsx, both ingetInitialPropsand inrender.pages/some-other-page.tsx, both ingetInitialPropsand inrender.Note how our code is in TypeScript but I didn't need to add
-r ts-node/registeror similar.If I had the
nextbinary installed globally, this should also work (note theNODE_OPTIONSapproach):But I haven't tried as we have Next.js as a devDependency.
๐ซ A method that doesn't work for me (but I generally like it) is having something like this in
package.json:Then run
yarn dev:debugit iTerm or similar and launch a "Node: Attach" config:This works for our plain Node.js projects but in a Next.js app, even though VSCode seemingly connects to the debugger (the status bar goes orange, the Debug Console mirrors console output, etc.), my breakpoints are not hit. I'm almost sure that it has something to do with the process being started through
yarnand not directly. If anyone knows how to fix this, I'd be interested.โ A workaround for this is, of course, to run the command directly:
(or
--inspect-brk).Client-side
โ If I don't need the familiarity and comfort of VSCode, by far the easiest is to just debug in Chrome directly. In DevTools, I go to Sources panel, press the familiar Cmd+P, browse to a file like
webpack://./pages/_app.tsx(yes, it's TypeScript directly) and set breakpoints there.โ Debugging via VSCode works as well but there's a bit of a setup. After installing this VSCode extension, the most annoying part is starting Chrome with remote debugging on, which on a Mac is done from the command line:
For this to work, Chrome must be shut down entirely or otherwise, the existing instance will be used, without the debugger. (I use Chrome Canary for development so that I don't need to mess with my main Chrome browser but still, it's inconvenient.)
When the setup is done, this launch config is enough to connect VSCode to Chrome:
It's also possible to launch Chrome directly:
Compound configuration
It's possible to run both server-side and client-side debugging in VSCode using a compound configuration, for example:
Custom server
With Next.js 9 dynamic routing, we no longer have a reason to use a custom server but previously, we had a config like this:
We also had a
dev:debugscript inpackage.jsonthat looked like this:I'm pretty sure it worked fine with the _Node attach_ configuration. Overall, custom server was not a problem. โ
Issues I know about
pages/post/[id].js: #8295Resources
--inspect