如何用nodemon在vscode中调试

最近被webstorm搞的只能和他说再见了,下面就说一下nodemon怎么在vscode中调试
2020-01-19更新:
如果vscode的launch.json不配置port的话,nodemon启动时会默认使用--inspect-brk参数,这个参数可以指定使用chrome调试时的Pid。
在使用typecript时我们需要先使用tsc编译,再启动,楼主尝试直接在nodemon.json里配置

"execMap": {  
    "js": "npm run start" 
  }

时,日志会打出以npm run start --inspect-brk=pid ./dist/app.js的日志,实际上我们期望的是工程以node --inspect-brk=pid ./dist/app.js 方式启动,因为这样vscode的调试系统才能获取Chrome DevTools并断点调试,为了解决这个问题,笔者想了个办法
在工程的bin目录下增加dev.sh内容如下

npm run prestart #先编译
node $1 ./dist/app.js #因为nodemon启动时默认带了--inspect-brk参数,此时获取该参数并启动可以实现pid的Attachs

然后修改nodemon.json如下

nodemon.json

{
  "restartable": "rs",
  "ignore": [
    ".git",
    "node_modules/**/node_modules"
  ],
  "verbose": true,
  "execMap": {  
    // "js": "npm run start" 
      "js": "./bin/dev.sh" // 改为dev.sh启动
  },
  "watch": [
    "src/"
  ],
  "ext": "ts,js,json"
}
 // 采用vscode默认配置
{
            "type": "node",
            "request": "launch",
            "name": "nodemon",
            "runtimeExecutable": "nodemon",
            "program": "${workspaceFolder}/dist/app.js",
            "restart": true,
            "console": "integratedTerminal",
            "internalConsoleOptions": "neverOpen",
            "skipFiles": [
                "<node_internals>/**"
            ],
            "sourceMaps": true,
            "outFiles": [
                "${workspaceFolder}/dist/**/*.js"
            ],
            "cwd": "${workspaceFolder}",
            "timeout": 25000
        }

搞定!


以下方法是在意识到上述问题之前采用的手段,也可以用,只是要操作两下:先npm run debug(其实就是用nodemon启动工程),然后再Attach nodemon启动的线程,一开始对vscode的这个Attach很是困惑,后来查了下Attach这个单词的意思:把…固定,把…附(在…上); 应该不用多说了吧。

第一步:debug->Add configuration

如图右下角Add configration可以自己定义配置

也可以复制我的

{

            "type": "node",

            "request": "attach",

            "name": "Attach by Process ID",

            "processId": "${command:PickProcess}",

            "skipFiles": [

                "<node_internals>/**"

            ],

            "protocol": "inspector",

            "restart": true,

        }
image

在package.json中加入script


"debug": "nodemon"

第二步:如何调试?

npm run debug

然后点击那只虫子

image
image

单步调试一下,OK了。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。