Skip to content

Instantly share code, notes, and snippets.

@borestad
Last active October 11, 2023 17:17
Show Gist options
  • Save borestad/6916dc7c00dc57c3307da4945d99cbf6 to your computer and use it in GitHub Desktop.
Save borestad/6916dc7c00dc57c3307da4945d99cbf6 to your computer and use it in GitHub Desktop.
Node Debugger demo
/*
Simple demo to show how the node inspector works
✨ 1: Run with:
node --inspect-brk debuggerdemo.js
By default, the debug process runs on "ws://127.0.0.1:9229", but you can change the port. See node --help for more info
--inspect[=[host:]port] activate inspector on host:port (default: 127.0.0.1:9229)
--inspect-brk[=[host:]port] activate inspector on host:port and break at start of user script
--debug-port, --inspect-port=[host:]port set host:port for inspector
✨ 2: Goto Chrome and open
chrome://inspect/#devices
Protip for mac users (or use https://github.com/sindresorhus/open-cli)
open -a 'Google Chrome' 'chrome://inspect/#devices' &; node --inspect-brk debuggerdemo.js
✨ 3: Click "Open dedicated DevTools for Node"
✨ 4: If your connection list is empty (or doesn't show 127.0.0.1:9229, please add it to the list by via "Add connection")
✨ 5: When DevTools fires up - check "Pause on uncaught exceptions" (we will need it to autopause later when global.foo.bar.baz() is executed)
✨ 6: Profit!
More reading:
https://nodejs.org/en/docs/guides/debugging-getting-started
https://nodejs.org/api/debugger.html
https://code.visualstudio.com/docs/nodejs/nodejs-debugging
https://github.com/debug-js/debug
https://github.com/pinojs/pino
https://medium.com/@paul_irish/debugging-node-js-nightlies-with-chrome-devtools-7c4a1b95ae27
*/
const {log} = console
function someLogic(x) {
return x + 1000
}
function someLoop() {
// Test loop 2
for (let j=0; j<10; j++) {
const someValue = someLogic(j)
}
}
log('Debug mode?', require('inspector').url())
const a = 1 + 2
const b = 3 + 5
log(b / 0) // <----------- Will not crash. Infinity is not considered an exception
log(a, b)
// Poor mans feature: the hardcoded debugger keyword (please don't leave this in production :)
debugger // <----------- The program will automatically stop here (if inspector is enabled)
// Test loop 1
for (let i=0; i<5; i++) {
log(i)
// Poor mans conditional breakpoint
if (i == 3) {
log('I have stopped')
debugger
}
}
// Simulating unknown logic without any logging
someLoop()
// What just happened here? And no logging. Oh noez. What to do?
// Protip:
// Let's use the fact that console.log returns undefined and
// put a conditional breakpoint in the inspector with `console.log(someValue)`
debugger
// Simulating unknown logic with conditional breakpoint logging
someLoop()
global.foo.bar.baz() // <----------- The program will crash here (if "Pause on uncaught exceptions" is toggled)
log('I will fail to be logged')
This file has been truncated, but you can view the full file.
@borestad
Copy link
Author

debuggerdemo.mp4

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment