Debugging Node.js server code in Visual Studio Code

この記事は約4分で読めます。
スポンサーリンク

Things I want to do

When implementing an HTTP server with Node.js, I debug it using Visual Studio Code.

スポンサーリンク

Environment

Node.js v20.16.0

VisualStudioCode 1.94.2

スポンサーリンク

debug

Select ‘Open Folder…’ from the File menu to open the folder containing your project.

Click ‘Run and Debug’ on the left side of the screen, then click ‘Create a Launch.json file’.

A dropdown menu will appear at the top of the screen; select Node.js.

After a short wait, a Launch.json file will be created in the project folder/.vscode with the following content.

The value of `program:` will vary depending on the project. Also, if it is a different file from the server-side entry point, it will be rewritten to the JavaScript file of the server-side entry point.

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "type": "node",
            "request": "launch",
            "name": "Launch Program",
            "skipFiles": [
                "<node_internals>/**"
            ],
            "program": "${workspaceFolder}\\server.mjs"
        }
    ]
}

Running the application

Click the Launch Program icon in the upper left corner of the screen.

Debug

You can set a breakpoint by clicking to the left of the line number in the code.

After breaking, you can view the call stack and variables, similar to Chrome’s Developer Tools.

Exit the app

Clicking the Stop icon at the top of the screen will close the application.

スポンサーリンク

Result

I was able to debug the server-side code in Node.js.

スポンサーリンク

Websites I used as references

Visual Studio CodeでNode.jsデバッグ - Qiita
Visual Studio Code Visual Studio Code でNode.jsがデバッグできるとのことだったので試してみました。 以前はCOBO...

コメント

タイトルとURLをコピーしました