Debugging is a crucial part of the development process, and Next.js 13 offers powerful features for both server-side rendering (SSR) and client-side interactions. While client-side debugging in the browser’s developer tools is familiar, debugging SSR code directly within your IDE can significantly enhance your workflow. This blog post will guide you through setting up VS Code for debugging both SSR and client-side code in your Next.js 13 application.
We’ll start by setting up a simple Next.js 13 CRUD application that operates on an in-memory array, allowing us to focus on the debugging configuration without the complexities of a database.
1. Clone the Sample Next.js 13 CRUD Project:
Open your terminal and navigate to your desired project directory. Then, clone the following repository:
Bash
1 2 3 | cd crud-example npm install # Or yarn install or pnpm install |
This repository contains a basic Next.js 13 application with CRUD operations implemented on an array.
2. Configure VS Code for Debugging:
VS Code utilizes a launch.json
file to manage debugging configurations. Follow these steps to create or modify this file:
- Open your Next.js project in VS Code.
- Go to the “Run and Debug” view (Ctrl+Shift+D or Cmd+Shift+D).
- Click on the “create a launch.json file” link (if you don’t have one already).
- Choose “Node.js” as the environment.
This will create a default launch.json
file. Now, replace its contents with the following configuration:
JSON
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | { "version": "0.2.0", "configurations": [ { "name": "Next.js: debug server-side", "type": "node-terminal", "request": "launch", "command": "npm run dev", "cwd": "${workspaceFolder}" }, { "name": "Next.js: debug client-side", "type": "chrome", "request": "launch", "url": "http://localhost:3000", "webRoot": "${workspaceFolder}" }, { "name": "Next.js: debug full stack", "type": "node-terminal", "request": "launch", "command": "npx next dev", "cwd": "${workspaceFolder}", "serverReadyAction": { "pattern": "- Local:.+(https?://.+)", "uriFormat": "%s", "action": "debugWithChrome" // Or "debugWithEdge", "debugWithFirefox" } } ] } |
Explanation of the launch.json
Configurations:
Next.js: debug server-side
:name
: A descriptive name for this configuration.type
: Specifies the debugger type, which isnode-terminal
for server-side Node.js debugging within the VS Code terminal.request
: Set tolaunch
to start a new debugging session.command
: The command to execute your Next.js development server.npm run dev
(oryarn dev
orpnpm dev
depending on your package manager) starts the Next.js development server.cwd
: Specifies the current working directory, which is the root of your project. This ensures the command is executed in the correct context.
Next.js: debug client-side
:name
: A descriptive name for client-side debugging.type
: Set tochrome
(oredge
,firefox
if you prefer) to attach the VS Code debugger to your browser. Make sure you have the corresponding debugger extension installed in VS Code (e.g., “Debugger for Chrome”).request
: Set tolaunch
to open a new browser window with the specified URL.url
: The URL of your Next.js application, typicallyhttp://localhost:3000
during development.webRoot
: Specifies the root directory of your web application. This helps the debugger map the code running in the browser back to your source files in VS Code.
Next.js: debug full stack
:name
: A convenient configuration for debugging both server-side and client-side code simultaneously.type
: Usesnode-terminal
to start the Next.js development server.request
:launch
to start a new session.command
: Usesnpx next dev
to ensure you’re using the locally installed Next.js version.cwd
: Sets the working directory to your project root.serverReadyAction
: This crucial section automates the client-side debugger attachment.pattern
: A regular expression that matches the line output by the Next.js development server indicating the local URL.uriFormat
: Specifies how to extract the URL from the matched pattern (%s
refers to the captured group).action
: Set todebugWithChrome
(ordebugWithEdge
,debugWithFirefox
) to automatically launch and attach the browser debugger once the server is ready.
3. Using the Debug Configurations:
Now that you have configured your launch.json
file, you can start debugging your Next.js 13 application:
Debugging Server-Side Code:
- Set breakpoints in your server-side code within the
app
directory (e.g., within a Route Handler in aroute.js
file or within a Server Component). - Go to the “Run and Debug” view in VS Code.
- Select “Next.js: debug server-side” from the dropdown menu.
- Click the “Start Debugging” button (the green play icon) or press F5.
- VS Code will start your Next.js development server in a debug session. When your server-side code with breakpoints is executed due to a client request, the debugger will pause at your breakpoints, allowing you to inspect variables, step through code, and evaluate expressions.
Debugging Client-Side Code:
- Set breakpoints in your client-side code within your components (e.g., within a Client Component in a
.js
or.jsx
file). - Go to the “Run and Debug” view.
- Select “Next.js: debug client-side” from the dropdown menu.
- Click the “Start Debugging” button or press F5.
- VS Code will launch a new Chrome window (or your configured browser) and navigate to
http://localhost:3000
. When your client-side code with breakpoints is executed in the browser, the debugger in VS Code will pause, allowing you to inspect the client-side state and behavior.
Debugging Full Stack (Server and Client Simultaneously):
- Set breakpoints in both your server-side and client-side code.
- Go to the “Run and Debug” view.
- Select “Next.js: debug full stack” from the dropdown menu.
- Click the “Start Debugging” button or press F5.
- VS Code will start your Next.js development server and automatically launch a Chrome window (or your configured browser) and attach the client-side debugger once the server is ready. Now, when execution hits your breakpoints on either the server or the client, VS Code will pause the debugging session.







Benefits of VS Code Debugging:
- Integrated Environment: Debug directly within your familiar code editor, eliminating the need to switch between the browser’s developer tools and your IDE for server-side debugging.
- SSR Debugging Capabilities: Unlike browser developer tools, VS Code allows you to step through and inspect your server-side rendering logic.
- Full Stack Debugging: The “debug full stack” configuration provides a seamless experience for debugging interactions between your server and client code.
- Enhanced Workflow: Streamline your debugging process and identify issues more efficiently.
By configuring VS Code with these launch.json
settings, you can significantly improve your Next.js 13 development experience by enabling powerful and integrated debugging capabilities for both server-side rendering and client-side code. Remember to install the necessary browser debugger extension in VS Code for client-side debugging to work correctly. Happy debugging!