Languages
[Edit]
EN

VS Code - debug Express.js under Node.js server

5 points
Created by:
troya
692

In this short article, we would like to show how to configure VS Code to debug Express.js applications.

Quick solution (create the following .vscode/launch.json configuration file):

{
    "version": "0.2.0",
    "configurations": [
        {
            "command": "node index.js",
            "name": "My Backend",
            "request": "launch",
            "type": "node-terminal"
        }
    ]
}

Hint: you can change "node index.js" to "nodemon index.js" to debug Expess.js with nodemon.

Screenshot:

VS Code debugger used with Express.js application.
VS Code debugger used with Express.js application.

Project structure

/C/
 |
 +-- back-end/
      |
      +-- .vscode/
      |    |
      |    +-- launch.json
      |
      +-- node_modules/
      +-- index.js
      +-- package-lock.json
      +-- package.json

index.js file:

const express = require('express');

const port = 3000;
const app = express();

app.get('/test', (request, response) => {
    response.send('This is test...');
});

app.listen(port, () => {
    console.log(`Server is listening at ${port} port.`);
});

package.json file:

{
  "name": "back-end",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "node index.js",
    "dev": "nodemon index.js"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "express": "^4.17.1",
    "nodemon": "^2.0.12"
  }
}

Node.js packages should be installed with the following command:

npm ci install

Where: ci parameter runs installation without changing the version of the used packages.

See also

  1. VS Code - debug js file with Node.js
  2. VS Code - debug with npm run

Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join