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

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

NodeJS中没有console.debug()方法。 这里是控制台对象的文档,所以你可以select最好的方法供你使用。

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

这就是说你可以扩展控制台对象来包含一个console.debug()方法,并且只有在debugging模式下才能打印控制台消息。

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

添加到@maninvan答案,我会使用variablesargs的语法:

 var isDebugMode = true; console.debug = function(/* ...args */) { if(isDebugMode) { var vargs = Array.prototype.slice.call(arguments); console.log.apply(this, vargs); } } // or ES6 style console.debug = (...args) => { if(isDebugMode) { console.log.apply(this, args) } }