Interactive debugging for NodeJS apps in Docker
Abhinav Sachdeva
Posted on September 10, 2024
Debugging can get hectic and with a bunch of console.log
s you could still be missing that one variable that needs to be printed.
Interactive debugging is nothing new for NodeJS developers, the Node devtools has been around for quite a while and here's how you can work with it while running your app inside docker.
Inside the docker compose:
- Expose the debugger port on your app
- Start the app with the inspect flag
services:
user-service:
image: node:18-alpine
ports:
- '3000:3000'
- '9228:9228' #Debugger port
working_dir: /app
volumes:
- ./src:/app/src
- ./node_modules:/app/node_modules
environment:
VAR1: value1
VAR2: value2
command: node --inspect=0.0.0.0:9228 ./src/index.js # Inspect mode
Head over to the Chrome browser and open up devtools, you should have the NodeJS devtools icon available.
If not, open chrome://inspect/
in Chrome and click on "Open dedicated DevTools for Node". Also, make sure that the URL and port you're connecting to are configured as discoverable.
BONUS TIP
If you'd like to have the VS Code debugging environment, add this file at the root of your workspace. Once added, goto the debugger panel and click on the play icon against the configuration name.
// .vscode/launch.json
{
"version": "0.2.0",
"configurations": [
{
"name": "Docker: Node.js",
"type": "node",
"request": "attach",
"port": 9228,
"localRoot": "${workspaceFolder}/src",
"remoteRoot": "/app/src",
"restart": true
}
]
}
Posted on September 10, 2024
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.