wmmhihaa
wmmhihaa

Reputation: 953

node.js - Capture debug output from module

I need to capture debug information while I make a require call to find out why some packages are not found.

In Module (internal module.js) there are several debug calls such as:

  if (parent) {
      debug('looking for %j in %j', id, paths);
  }

What do I need to do to capture this debug information?

Thanks

Upvotes: 0

Views: 496

Answers (1)

robertklep
robertklep

Reputation: 203304

debug() is a function that was created using util.debuglog(), which means that if you set the correct environment variable, the debug messages will be written to stderr, which you can then capture to a file (for instance):

env NODE_DEBUG=module node your-app.js 2> debug.log

EDIT: to capture these messages from within your own app, I think you have to resort to monkeypatching, for instance console.error()

let error = console.error.bind(console);
console.error = function() {
  if (arguments[1] !== 'MODULE') {
    return error.apply(this, arguments);
  }
  console.log('error log', arguments);
};

This code needs to run before any of the require() statements that you want to track, and it's not very robust (if the implementation of util.debuglog ever changes, it might break).

Upvotes: 2

Related Questions