Skip to content

Instantly share code, notes, and snippets.

@AriPerkkio
Last active March 8, 2023 09:43
Show Gist options
  • Save AriPerkkio/ffd517437c50e64ccdc875b5e3553089 to your computer and use it in GitHub Desktop.
Save AriPerkkio/ffd517437c50e64ccdc875b5e3553089 to your computer and use it in GitHub Desktop.
Node inspector worker_threads + child_process
/*
* Run as `node process.mjs` and open Chrome DevTools or similar debugging tool
*/
import { fork } from "node:child_process";
import { fileURLToPath } from "node:url";
import inspector from "node:inspector";
const filename = fileURLToPath(import.meta.url);
if (!process.env.SOME_FLAG) {
fork(filename, {
env: { SOME_FLAG: "true" }, // Just to indicate when running this file inside child_process
execArgv: [/* Just to indicate that no --inspect-brk is passed to process */],
});
} else {
inspector.open();
inspector.waitForDebugger();
// Stopping in child process
debugger;
}
/*
* Run as `node threads.mjs` and open Chrome DevTools or similar debugging tool
*/
import { isMainThread, Worker } from "node:worker_threads";
import { fileURLToPath } from "node:url";
import inspector from "node:inspector";
const filename = fileURLToPath(import.meta.url);
if (isMainThread) {
new Worker(filename, {
execArgv: [/* Just to indicate that no --inspect-brk is passed to worker */] });
} else {
inspector.open();
inspector.waitForDebugger();
// Stopping in worker thread!
debugger;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment