4

I have some issue with Visual studio code with Cluster

Edit

If I hit Ctrl + F5 it works correctly, what it's doing other than just F5, do I need to start command always with Ctrl?

---

It seems like workers never starts when started with VS Code Launch command (F5). Do I need to do some changes to .vscode/launch.json file to make Cluster work proberly.

Actual code is copied from Node.js 6 api https://nodejs.org/api/cluster.html#cluster_cluster

npm test Windows Command prompt shows this:

Master started
Listening port 80
Listening port 80
Listening port 80
Listening port 80

VS Code (F5) Debug Console show this:

node --debug-brk=7601 --nolazy index.js
Debugger listening on port 7601
Master started
Debugger listening on port 7602
Debugger listening on port 7603
Debugger listening on port 7604
Debugger listening on port 7605

VS Code launch.json

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Launch",
            "type": "node",
            "request": "launch",
            "program": "${workspaceRoot}/index.js",
            "stopOnEntry": false,
            "args": [],
            "cwd": "${workspaceRoot}",

    ..........

index.js

const cluster = require('cluster');
const http = require('http');
const numCPUs = require('os').cpus().length;

if (cluster.isMaster) {
    // Fork workers.
    console.log('Master started')
    for (var i = 0; i < numCPUs; i++) {
        cluster.fork();
    }

    cluster.on('exit', (worker, code, signal) => {
        console.log(`worker ${worker.process.pid} died`);
    });
} else {
    // Workers can share any TCP connection
    // In this case it is an HTTP server
    http.createServer((req, res) => {
        res.writeHead(200);
        res.end('hello world\n');
    }).listen(80);
    console.log('Listening port 80')
}
EspeH
  • 1,308
  • 2
  • 16
  • 34

2 Answers2

1

I had the same issue. The 2nd workaround described by weinand in https://github.com/Microsoft/vscode/issues/3201 works for me:

Launch node from a terminal and attach to it with the VS Code debugger.

Run in terminal: node --debug app.js

Then select the default 'attach' launch config and attach to it.

The workaround is the preferred way if you actually want to debug any worker and not just the first process that is launched.

Krzysztof
  • 41
  • 1
  • 4
  • for development and only debugging the master you can do: process.execArgv[0] = process.execArgv[0].replace('-brk', ''); – ucipass Apr 27 '17 at 12:45
0

I have the same problem on Visual studio code with Cluster.

I found that there are some dirty method to make it works.

Mac OS X:

/Applications/Visual Studio Code.app/Contents/Resources/app/extensions/node-debug/out/node/nodeDebug.js

Windows:

C:\Program Files (x86)\Microsoft VS Code\resources\app\extensions\node-debug\out\node\nodeDebug.js

Change this

if (!this._noDebug) {
    launchArgs.push("--debug-brk=" + port);
}

to

if (!this._noDebug) {
    launchArgs.push("--debug=" + port);
}

I know it is not the best way to solve it, but it is working so far for me.