How to debug child Node.JS process in Visual Studio Code?

前端 未结 5 2398
别那么骄傲
别那么骄傲 2021-02-19 19:42

How to debug child Node.JS process in VS Code?
Here is the example of the code that I\'m trying to debug:

var spawn = require(\'child_process\').spawn;
var s         


        
5条回答
  •  日久生厌
    2021-02-19 20:00

    Look for this npm module child-process-debug.

    I created 2 separate launch configurations in vscode:

    One for master process, other for child process

       {
            "name": "Attach",
            "type": "node",
            "request": "attach",
            "port": 5858,
            "address": "localhost",
            "restart": false,
            "sourceMaps": false,
            "outFiles": [],
            "localRoot": "${workspaceRoot}",
            "remoteRoot": null
        },
        {
            "name": "Attach child",
            "type": "node",
            "request": "attach",
            "port": 5859,
            "address": "localhost",
            "restart": false,
            "sourceMaps": false,
            "outFiles": [],
            "localRoot": "${workspaceRoot}",
            "remoteRoot": null
        }
    

    Workflow as follows:

    1. Start master node process with --debug command line switch $ node --debug master.js
    2. Attach to master.js node process using Attach via debug panel
    3. Place break point in the child.js process
    4. Quickly detach from main process and attach to child process using Attach child

    Fro debugging purposes, you may delay message sending between processes using setTimeout

    // master.js
    var child = child_process.fork(__dirname + './child.js')
    setTimeout(function() {
        child.send('...')
    }, 5000)
    

提交回复
热议问题