控制台在 Nodejs 中没有“调试”方法?

新手上路,请多包涵

console.debug() 函数可以在浏览器控制台调用。

但是在Nodejs中调用 console.debug() 时出现一个错误。

 TypeError: Object #<Console> has no method 'debug'
    at Object.<anonymous> (c:\share\node\receive.js:20:9)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Function.Module.runMain (module.js:497:10)
    at startup (node.js:119:16)
    at node.js:929:3

为什么?有什么方法可以在 Nodejs 中替换 console.debug 吗?

原文由 zangw 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 269
2 个回答

NodeJS 中没有 console.debug() 方法。 是控制台对象的文档,因此您可以选择最适合您的方法来使用。

原文由 Morry 发布,翻译遵循 CC BY-SA 3.0 许可协议

正如前面的回答所述,node.js 中没有 console.debug() 方法。使用日志、信息、警告、错误方法 https://nodejs.org/api/console.html

也就是说,您可能需要扩展控制台对象以包含 console.debug() 方法,并且仅在调试模式下使用此方法打印控制台消息。

 var isDebugMode = true;

console.debug = function(args)
{
  if (isDebugMode){
    console.log(args);
  }
}

原文由 maninvan 发布,翻译遵循 CC BY-SA 3.0 许可协议

推荐问题