0

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

1 Answer 1

2

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).

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. Is it possible to capture stderr within the node.js application?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.