How to debug a nodemon project in VSCode

后端 未结 6 1853
醉酒成梦
醉酒成梦 2021-02-04 01:19

I have a NodeJs project and I run it using nodemon,
I wish to run it in debug mode for development tasks, but I am unable to do so.

I found that I\'ll need to ad

6条回答
  •  没有蜡笔的小新
    2021-02-04 02:06

    I ran into a similar issue attaching to a Dockerized nodemon process. I found the solution in this article. I was able to get it working by changing three things:

    1. Added --inspect=0.0.0.0 to the npm script that launched the service (named debug in my case):
      "scripts": {
        "debug": "nodemon -w lib -w server.js --inspect=0.0.0.0 server.js"
      }
    
    1. Making sure port 9229 (or whatever debug port you wish to use) is open in the Docker container. I'm using docker-compose, so it's defined in the associated yaml:
    ports:
      - "8080:8080"
      - "9229:9229"
    
    1. Adding the following configuration to launch.json in VS Code:
        "configurations": [
            {
                "name": "Attach to Node in Docker",
                "type": "node",
                "address": "localhost",
                "port": 9229,
                "request": "attach",
                "restart": true
            }
        ]
    

    The "restart": true option allows the debugger to automatically reattach when nodemon recompiles things after a watched file changes.

提交回复
热议问题