【问题标题】:What else should I use for an alternative to console in nodejs?我还应该使用什么来替代 nodejs 中的控制台?
【发布时间】:2017-02-07 17:31:30
【问题描述】:

我正在使用tslint:recommended 规则集编写一个Node.js 应用程序,它会警告我在我的代码中使用console.log

src/index.ts[1, 1]: Calls to 'console.log' are not allowed.

我还应该使用什么?我应该使用某种system.out 等价物吗?

这是我的tslint.json

{
  "extends": "tslint:recommended"
}

【问题讨论】:

    标签: node.js typescript tslint


    【解决方案1】:

    eslint doc 中有关于no-console 规则的说明:

    什么时候不使用它

    但是,如果您使用的是 Node.js,控制台用于向用户输出信息,因此不严格用于 调试目的。如果您正在为 Node.js 开发,那么您最 可能不希望启用此规则。

    因此,这里偏离推荐的规则集是有效的,因此我调整了我的tslint.json 以匹配:

    {
        "extends": "tslint:recommended",
        "rules": {
            "no-console": false
        }
    }
    

    【讨论】:

    • false 应该是 0(至少我的 eslint 版本是这种情况)。
    • 这是 tslint 不是 eslint
    • @jeznag 这不影响何时不使用no-console 规则的推理。
    【解决方案2】:

    console.log 的更快替代方案: process.stdout.write("text");

    您需要自己插入换行符。示例:

    process.stdout.write("new line\n");

    Difference between "process.stdout.write" and "console.log" in node.js?

    【讨论】:

      猜你喜欢
      • 2010-11-05
      • 2010-12-06
      • 2012-01-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-06
      相关资源
      最近更新 更多