Alexander Mills
Alexander Mills

Reputation: 99960

ignore debugger; statements in Node.js, during debug session

I have a codebase where I have quite a number of

debugger;

statements. Sometimes I debug, and I actually just want to skip all of the debugger; statements and continue only to the manually set breakpoints that I have chosen for the debugging session, is there some setting by chance with Node.js to do that?

In other words, I would like to consider the debugger; statements to be long-term placeholders, but for certain debugging sessions I would like to ignore those long-term placeholders.

Upvotes: 8

Views: 1356

Answers (3)

Casper Beyer
Casper Beyer

Reputation: 2301

Quick and dirty way (its for debug so, its fine really) is to stick something like the following script in scripts/debugger.js

require.extensions['.js'] = function(module, filename) {
  var content = fs.readFileSync(filename, 'utf8').replace(/debugger/g, [
    '(function() {',
    '  if (__debugger) {',
    '    debugger;',
    '  }',
    '})',
  ].join('\n'));

  module._compile(content, filename);
};

then start node with node -r ./scripts/debugger

Using a global variable here so that it can be enabled/disabled from the debugger repl or chrome's repl if debugging with --inspect.

Technically require.extensions is deprecated, but it's not going to be removed and it works as intended here.

Upvotes: 3

intentionally-left-nil
intentionally-left-nil

Reputation: 8274

A trick I've used in the past is to just use babel to strip out debugger statements:

See: https://www.npmjs.com/package/babel-plugin-remove-debugger

Upvotes: 3

Hosar
Hosar

Reputation: 5292

That can be done with the chrome devtools.

You can do:

node --inspect --debug-brk index.js

that will generate something like this:

chrome-devtools://devtools/remote/serve_file/@60cd6e859b9f557d2312f5bf532f6aec5f284980/inspector.html?experiments=true&v8only=true&ws=localhost:9229/9c2e4f37-4c3a-4477-b8da-2399c5d9819e

Just copy and paste that on chrome.

There is an option to disable/enable all the break points, and chrome will remember all the breakpoints that you set previously.

Please check: --inspect for more info.

Upvotes: 6

Related Questions