1 nodejs_debug
jason.zhu edited this page 2021-05-11 09:26:52 +10:00

Debug nodejs in container

There are multiple ways of debugging within nodejs, from most manual one to utilize vscode .devcontainer feature

Method 1: Manual start debugger

Checks there is a launch.json file within .vscode, with following conten

        {
            "name": "Docker: Attach to Node.js",
            "type": "node",
            "request": "attach",
            "remoteRoot": "/work/src/",
            "port": 9229,
            "address": "localhost",
            "localRoot": "${workspaceFolder}/nodejs/src",
            "protocol": "inspector",
            "restart": true,
        },
  1. Make sure CMD ["nodemon", "--inspect=0.0.0.0:9229", "./src/server.js"] within dockerfile is commented out.
  2. Build and run nodejs container
  3. In a separate terminal, docker exec -it nodejs sh to attach shell to container.
  4. Manually start debugger process in container nodemon --inspect=0.0.0.0:9229 ./src/server.js
  5. In vscode debugger, select the debugger profile "Docker: Attach to Node.js", and start debugger by link to the port 9229
  6. Set breaker in the codes
  7. In Chrome, open localhost:5502 to the open port
  8. Should notice that program will stop at breakpoint

Method 2:

Reading lists