Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What else should I use for an alternative to console in nodejs?

I am writing a Node.js application using the tslint:recommended rule set and it warns me about the usage of console.log in my code:

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

What else should I be using? Should I use some sort of system.out equivalent?

Here's my tslint.json:

{
  "extends": "tslint:recommended"
}
like image 285
k0pernikus Avatar asked Sep 29 '16 11:09

k0pernikus


People also ask

Does node have a console?

The node:console module provides a simple debugging console that is similar to the JavaScript console mechanism provided by web browsers. The module exports two specific components: A Console class with methods such as console.

Can we use console log in node JS?

js provides a console module which provides tons of very useful ways to interact with the command line. It is basically the same as the console object you find in the browser. The most basic and most used method is console. log() , which prints the string you pass to it to the console.


Video Answer


2 Answers

It is stated in the eslint doc about the no-console rule:

When Not To Use It

If you’re using Node.js, however, console is used to output information to the user and so is not strictly used for debugging purposes. If you are developing for Node.js then you most likely do not want this rule enabled.

So it is valid to deviate from the recommended ruleset here and hence I adapted my tslint.json to match:

{
    "extends": "tslint:recommended",
    "rules": {
        "no-console": false
    }
}
like image 126
k0pernikus Avatar answered Sep 25 '22 00:09

k0pernikus


Faster alternative to console.log: process.stdout.write("text");

You will need to insert newlines yourself. Example:

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

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

like image 35
jaggedsoft Avatar answered Sep 25 '22 00:09

jaggedsoft