From ee839ce4f604f9f05cf7ce531b512fd0e3b81ce9 Mon Sep 17 00:00:00 2001 From: "jason.zhu" Date: Tue, 11 May 2021 09:26:52 +1000 Subject: [PATCH] Working on nodejs_debug --- nodejs_debug.md | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 nodejs_debug.md diff --git a/nodejs_debug.md b/nodejs_debug.md new file mode 100644 index 0000000..5a9ea62 --- /dev/null +++ b/nodejs_debug.md @@ -0,0 +1,39 @@ +# 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 + +```json + { + "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 + +* [VSCode DOC: Node.js in a container](https://code.visualstudio.com/docs/containers/quickstart-node) +* [VSCode DOC: Launch versus attach configuration](https://code.visualstudio.com/Docs/editor/debugging#_launch-versus-attach-configurations) +* [VSCode DOC: Remote debugging](https://code.visualstudio.com/docs/nodejs/nodejs-debugging#_remote-debugging) +* [VSCode DOC: Developing inside a container](https://code.visualstudio.com/docs/remote/containers) \ No newline at end of file