58

In node.js you can use console.log or sys.puts to print out to the screen.

What is the preferred method and what is the difference between these?

pimvdb
  • 151,816
  • 78
  • 307
  • 352
never_had_a_name
  • 90,630
  • 105
  • 267
  • 383

7 Answers7

61

sys.puts simply prints the given string in the logs.

But if you want to print a more complex object (Array, JSON, JSObject) you have to use console.log because you want to "look inside" of the object.

sys.puts would give you only "[object object]" for example.

Alberto Zaccagni
  • 30,779
  • 11
  • 72
  • 106
Elias
  • 3,300
  • 4
  • 34
  • 38
  • 23
    yes, however you don't HAVE to use console logs to see the object. sys.puts("check out this funky object in detail: " + sys.inspect(yourobject)); – fullstacklife Oct 19 '10 at 15:20
16

Both just write to the stdout stream. The difference is that sys.puts just toString's the first argument, and console.log takes multiple arguments, and will sys.inspect the first arg if it's not a string.

Philipp Kyeck
  • 18,402
  • 15
  • 86
  • 123
isaacs
  • 16,656
  • 6
  • 41
  • 31
9

Puts is deprecated in since version 0.2.3

angry kiwi
  • 10,730
  • 26
  • 115
  • 161
3

Also you can use console.log without requiring the sys module.

daralthus
  • 13,723
  • 2
  • 16
  • 13
2
console.dir(objectToInspect)

This might be another way to inspect objects.

Rajkamal Subramanian
  • 6,884
  • 4
  • 52
  • 69
1
sys.puts([...]);

Is a synchronous output function. Simply it is a blocking function as in Node.js language.

console.log([data], [...]);

Prints to stdout with newline.

For more Info:

http://nodejs.org/api/stdio.html#stdio_console_log_data

http://nodejs.org/api/util.html#util_util_puts

Note: 'sys' module was renamed to be 'util' (Supported by link) It was a Fix #3577

Amol M Kulkarni
  • 21,143
  • 34
  • 120
  • 164
0

The preferred method is console.log(). sys.puts() has been removed.


The sys module was renamed util with this commit dated Oct 12, 2010. So sys.puts() became util.puts(). util.puts() was added in v0.3.0, deprecation since v0.11.3, and removed in v12.0.0. The documentation advises to use console.log() instead.

d4nyll
  • 11,811
  • 6
  • 54
  • 68