EN
VS Code - debug Express.js under Node.js server
5
points
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 withnodemon
.
Screenshot:
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.