brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: node --inspect-brk pauses a Node script on its first line

Javascript
~/tips/tip-node-inspect-brk

node --inspect-brk app.js

Pause a Node.js script on line one so a debugger can attach before anything runs

A script that fails during startup is hard to debug, because by the time you attach a debugger the code has already run past the problem.

Start Node with --inspect-brk instead of plain --inspect:

node --inspect-brk app.js

Node opens the inspector on port 9229 and waits. Nothing in your script runs until a debugger connects, and then it stops on the first line. Open chrome://inspect in Chrome and click inspect under your script, or attach the debugger of your editor. From there you can set breakpoints and step through the startup code.

You can pick another port with node --inspect-brk=9230 app.js, which helps when two scripts run at once. For an npm script, pass the flag through node --inspect-brk node_modules/.bin/tool or set NODE_OPTIONS carefully, since it affects every child process.

A caveat: the inspector listens on localhost by default, which is what you want. Don’t bind it to 0.0.0.0 on a machine others can reach, because anyone who connects can run code inside your process.

Further reading: Node.js debugging guide

More Javascript tips obj?.prop const filePath = String.raw`C:\Development\profile\about.html`; const observer = new ResizeObserver((entries) => { URL.parse("not a url"); // null shareButton.addEventListener("click", async () => { All Javascript tips (40) All topics

Link