Search code examples
google-chromedebuggingbreakpoints

Launch Chrome with JS scripts suspended at startup


In NodeJS, you can run a script with the --debug-brk option, which pauses your script at the first line. You can then attach with a debugger and resume, ensuring you can hit any breakpoint no matter how early it is in your script.

Is there something similar in Chrome / Chrome Debugging Protocol?

I use an external debugger to debug JS in Chrome. Chrome is launched via command-line, with remote debugging enabled, pointing to a local server with my website / app. Once Chrome is launched, the debugger attaches to the page, but that takes time - by the time the debugger is attached, the scripts have pretty much already executed, meaning I cannot hit breakpoints that are early in the website / app's life cycle.

I thought of adding debugger; statements at the beginning of every script, but that would be annoying over time. I also think that the Chrome dev tools need to be open for debugger; statements to actually pause, so that might not even work.


Solution

  • In Chrome DevTools, you can use 'Event Listener Breakpoints' to force the execution to pause on 'Script First Statement'. There are others, such as load, which happens a little later on. You can then resume execution until the relevant breakpoints should be hit.

    Event Listener Breakpoints