Tag: Debug
Debugging NextJS 14 with VSCode

Debugging NextJS 14 with VSCode

Using debugging tools when you're developing is something that in my mind is absolutely essential, and I'm shocked whenever I see people response.writing out values to work out what's going wrong in there application. In the past I've written about debugging VueJS in VS Code and the setup for NextJs is somewhat similar.

With VS Code you have launch.json file which instructs how VS Code should start an application in debug mode when you hit F5.

Launch.json for NextJs 14 using app router

The launch.json file for NextJs 14 is very simple and consists of the following:

2 "version": "0.2.0",
3 "configurations": [
4 {
5 "name": "Next.js: debug full stack",
6 "type": "node-terminal",
7 "request": "launch",
8 "command": "npm run dev",
9 "serverReadyAction": {
10 "pattern": "- Local:.+(https?://.+)",
11 "uriFormat": "%s",
12 "action": "debugWithChrome"
13 }
14 }
15 ]

Launch.json for NextJs 13 using page router

The launch.json file for NextJs13 is slightly different. This one also includes an attach option which will attach to the running process if you've already manually started NextJs from the command line.

2 "version": "0.2.0",
3 "configurations": [
4 {
5 "name": "Next.js: debug full stack",
6 "type": "node-terminal",
7 "request": "launch",
8 "command": "npm run dev",
9 "console": "integratedTerminal",
10 "serverReadyAction": {
11 "pattern": "started server on .+, url: (https?://.+)",
12 "uriFormat": "%s",
13 "action": "debugWithChrome"
14 }
15 }
16 ]

This file will also work for NextJs 14, however I found that any component marked as "use client" doesn't hit any of the breakpoints in client side code. It also doesn't trigger chrome to open when the application starts.

Debugging VueJS + TypeScript with VS Code - Part 2

Debugging VueJS + TypeScript with VS Code - Part 2

In the past I have written about how to setup VS Code to debug a VueJS + TypeScript project. Since writing that article it's a method I've continued to use and it works well. It's quick to spin up and quite reliably allows you to place breakpoints in code.

However one aspect of it that I don't like is it's not so much "Run and Debug" from VSCode, it's more just the debug part, as to use it you must first go to a terminal and run your VueJS application.

There's two problems with this:

1. Most of the time you will just run the application not debugging (because why debug when you don't have an issue), therefore when you need it, you have to redo what you just did that caused an error. Instinctively there is then a desire to guess at what was wrong rather than going to the extra effort of debugging (frequently when you do this your guess is wrong and you end up spending more time guessing at what was wrong than using the tool that will tell you).

2. As the debugger generally isn't running, exceptions never get flagged up, and unless you have the browser console open (and check it), you can remain oblivious to something going wrong in your app.

There is a solution though, and it's quite simple!

Using VS Code to launch via npm

First follow through my previous guide on debugging a VueJS and TypeScript application.

Next, in your launch.config file add a new configuration with the definition below. This will run the command in a debug terminal, effectively doing the same thing as you typing npm run serve.

2 "command": "npm run serve",
3 "name": "Run npm serve",
4 "request": "launch",
5 "type": "node-terminal"
6 },

To get both our new and old configuration to run you can add a compound definition, that does both at the same time.

Here's mine.

1"compounds": [
2 {
3 "name": "Run and Debug",
4 "configurations": ["Run npm serve", "vuejs: edge"]
5 }
6 ],

My complete file now looks like this. Note you don't need configurations for edge and chrome, just use the one for the browser you use.

2 // Use IntelliSense to learn about possible attributes.
3 // Hover to view descriptions of existing attributes.
4 // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
5 "version": "0.2.0",
6 "compounds": [
7 {
8 "name": "Run and Debug",
9 "configurations": ["Run npm serve", "vuejs: edge"]
10 }
11 ],
12 "configurations": [
13 {
14 "command": "npm run serve",
15 "name": "Run npm serve",
16 "request": "launch",
17 "type": "node-terminal"
18 },
19 {
20 "type": "pwa-msedge",
21 "request": "launch",
22 "name": "vuejs: edge",
23 "url": "http://localhost:8080",
24 "webRoot": "${workspaceFolder}",
25 "breakOnLoad": true,
26 "sourceMapPathOverrides": {
27 "webpack:///./*": "${webRoot}/*"
28 },
29 "skipFiles": ["${workspaceFolder}/node_modules/**/*"]
30 },
31 {
32 "type": "chrome",
33 "request": "launch",
34 "name": "vuejs: chrome",
35 "url": "http://localhost:8080",
36 "webRoot": "${workspaceFolder}",
37 "breakOnLoad": true,
38 "sourceMapPathOverrides": {
39 "webpack:///./*": "${webRoot}/*"
40 },
41 "skipFiles": ["${workspaceFolder}/node_modules/**/*"]
42 }
43 ]

Now whenever you want to run the application, just run the compound Run and Debug and your VueJS app will start up and launch in your browser.

Running multiple projects from Visual Studio

Running multiple projects from Visual Studio

We're probably all familiar with the debugging experience in Visual Studio. Press F5 or hit the green play button and your application will run in debug mode. If you have multiple projects in you solution which can run (i.e. they're not a class library) then the one with a bold title in solution explorer will run.

To change which project starts by default right click the project and choose Set as Startup Project.

Selecting start up project in Visual Studio

You can also use the drop down next to the play button to switch which project will run when you start debugging.

Selecting project to run in Visual Studio

Running multiple project at the same time

However switching between projects might not be what your after. You may need multiple projects running that interact with each other. e.g. You could have an API written as an Azure Function and a Website which uses the API. The simple option is to just open to copies of Visual Studio and run one in each, but there is a better way.

Right click your solution in Solution Explorer and click properties.

Selecting properties for solution in Visual Studio

Make sure you have Startup project selected on the left. On the right change the radio to Multiple startup projects and in the grid use the drop down to change the action to either Start or Start without debugging on the projects you want to run.

Visual Studio selecting multiple startup projects

The drop down next the play button will now show that multiple startup projects have been selected.

Visual Studio multiple startup projects selected

Now when you start to debug all the projects you have set to start will all debug together for one seamless debugging experience. Just make sure they're not all running on the same port number.

Debugging VueJS + TypeScript with VS Code

Debugging VueJS + TypeScript with VS Code

I recently asked a developer working with VueJS how they do debugging and they told me console.log. Not wanting to go back to the dark ages of development I went and found the solution myself.

The VueJS website has a guide on how to do debugging with VS Code https://vuejs.org/v2/cookbook/debugging-in-vscode.html however the instructions they give don't work with TypeScript. Interestingly it does get you far enough to set breakpoints within either Chrome or Edge, but I don't really count that as a solution. The idea of debugging is you step through your code and see everything that is happening, not step through a copy of your code and then go and find the corresponding file in your code editor.

After a bit of digging I managed to get it to work, so here's my solution.


This part is essentially as per the VueJS guide.

Make sure you have VS Code installed and install the Debugger for Chrome. Don't worry if you use Edge, the same extension will work.

Create your project with the vue-cli, following the instructions in the Vue CLI Guide. When you do this make sure you pick Typescript.

Displaying Source Code in the Browser

This isn't what we're after but it's still a step we must do.

Create a file called vue.config.js in the root of your solution and paste the following into it. This creates the mapping for the debugger to map compressed files back to the original.

1module.exports = {
2 configureWebpack: {
3 devtool: 'source-map'
4 }

Configure debugging from VS Code

In VS Code select the Run and Debug button on the left and click the Run and Debug button. If you don't see this button it probably means that you already have a launch.json file defined. Instead a cog will appear at the top to edit the existing settings.

VS Code Run and Debug

From the options choose either Chrome or Edge.

VS Code select Chrome

This will create a launch.json file with some defaults filled out. Replace the contents of the file with the below (this is the part which differs from the instructions on VueJS's site).

I have included 2 configurations, one for debugging with Chrome and the other for Edge. They are both essentially the same and just launch a different browser.

2 // Use IntelliSense to learn about possible attributes.
3 // Hover to view descriptions of existing attributes.
4 // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
5 "version": "0.2.0",
6 "configurations": [
7 {
8 "type": "pwa-msedge",
9 "request": "launch",
10 "name": "vuejs: edge",
11 "url": "http://localhost:8080",
12 "webRoot": "${workspaceFolder}",
13 "breakOnLoad": true,
14 "sourceMapPathOverrides": {
15 "webpack:///./*": "${webRoot}/*"
16 },
17 "skipFiles": [
18 "${workspaceFolder}/node_modules/**/*"
19 ]
20 },
21 {
22 "type": "chrome",
23 "request": "launch",
24 "name": "vuejs: chrome",
25 "url": "http://localhost:8080",
26 "webRoot": "${workspaceFolder}",
27 "breakOnLoad": true,
28 "sourceMapPathOverrides": {
29 "webpack:///./*": "${webRoot}/*"
30 },
31 "skipFiles": [
32 "${workspaceFolder}/node_modules/**/*"
33 ]
34 }
35 ]

Debug from VS Code

To start your debugging you still need to start the application from a terminal with npm run serve. The debugging experience is more like Visual Studios attaching to a process rather than running application by clicking start.

Once your application is running, to attach the debugger either press either F5 or go to the run and debug tab, make sure your browser of choice is selected and click the green play button. Your browser of choice will now open and VS Code should be attached and any break points you create will be hit.