【问题标题】:Console has no method 'debug' in Nodejs?控制台在 Nodejs 中没有“调试”方法?
【发布时间】:2015-05-03 22:25:10
【问题描述】:

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

【问题讨论】:

  • 请将相关的节点版本添加到这个问题中,因为在更新的版本中行为已经不止一次地改变了

标签: javascript node.js console


【解决方案1】:

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

【讨论】:

  • 请注意,console.debug() 存在于 Node v8(可能还有 v7)中,但它似乎是无操作的:该函数调用时没有错误,但 stdout 或 stderr 上没有内容。
  • 该死,我的图书馆坏了。
  • documentation: v8.10.0 console.debug 现在是 console.log 的别名。
【解决方案2】:

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

也就是说,您可以扩展控制台对象以包含一个 console.debug() 方法,并且只有在处于调试模式时才让此方法打印控制台消息。

var isDebugMode = true;

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

【讨论】:

  • 请注意,info 只是 log 的别名,warn 只是 error 的别名。与名称所暗示的不同,在 Node 中您不能设置各种级别的日志记录。
【解决方案3】:

添加到@maninvan 答案,我会使用变量 args 语法:

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

【讨论】:

    【解决方案4】:

    由于节点9.3.0console.debug 现在将默认显示在标准输出中。


    旧解决方案:

    console.debug() 已添加到节点 v8.0.0 中,但通常不会打印到标准输出。

    您可以使用--inspect--inspect-brk 命令行标志查看console.debug 消息,并使用devtools 控制台选项卡中的“级别”下拉菜单启用详细日志记录。

    不幸的是,这并没有取消console.debug 的输出到标准输出。

    从节点 v9.2.0 开始,除了用 console.log 包装器替换函数之外,我不知道有任何方法可以在标准输出中查看 console.debug 的输出。

    【讨论】:

      【解决方案5】:

      节点 8.10.0 及更高版本已添加对 console.debug() 的支持

      https://github.com/nodejs/node/commit/162ff56439

      【讨论】:

        【解决方案6】:

        正如documentation 历史所说:

        v8.10.0 console.debug 现在是 console.log 的别名

        虽然用您自己的函数覆盖它很容易,但我更喜欢扩展 console 并使用我自己的 debug 方法,如下例所示:

        xconsole = Object.create(console);
        
        xconsole.debug = function (line) {
                if (process.env.NODE_ENV === "development") {
                        console.log(line);
                }
        }
        

        这使console.debug 保持不变。我在自己的文件中有这个,我import 可以在其他模块中使用。我可以改用新功能,如下所示:

        xconsole.debug("This will only show if NODE_ENV is set to development");
        xconsole.log("This will always show");
        

        当然,许多变化都是可能的。请参阅NODE_ENV 上的info

        我使用这种方法方便地在 docker 容器上启用调试模式。我的~/.bashrcpropagate the environment variables 有一个别名,因为我需要使用sudo 运行docker:

        alias docker='sudo -E docker'
        

        如果我需要启用调试模式,就像export NODE_ENV=development 一样简单。测试:

        docker run --name test -d -e NODE_ENV node:17-bullseye env
        

        我可以看到NODE_ENV=developmentdocker logs test。在exporting environment variables to root (-E) 时注意安全性很好,但这对我的开发环境来说很好。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-03-16
          • 2012-09-17
          • 1970-01-01
          • 1970-01-01
          • 2018-06-10
          相关资源
          最近更新 更多