node.js - 从模块

时间:2017-07-30 18:39:27

标签: javascript node.js debugging

我需要在进行 require 调用时捕获调试信息,以找出找不到某些包的原因。

模块(内部模块.js)中,有几个调试调用,例如:

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

我需要做些什么来捕获此调试信息?

由于

1 个答案:

答案 0 :(得分:2)

debug()是使用util.debuglog()创建的函数,这意味着如果设置了正确的环境变量,调试消息将写入stderr,然后您可以捕获到文件(例如):

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

编辑:要从您自己的应用中捕获这些消息,我认为您必须采用monkeypatching,例如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);
};

此代码需要在您要跟踪的任何require()语句之前运行,并且它不是非常健壮(如果util.debuglog的实现发生了变化,它可能会中断)

相关问题