console.log で改行させたくない

node: v8.2.1 でのはなし。

JavaScript でログを出力するときは console.log が一般的だけど、これだと改行が入ってしまう。

console.log('hoge')
console.log('hoge')

# hoge
# hoge

そうじゃなくて、 hogehoge って続けてログ出力してほしい。

ググる

node.jsで標準出力に改行なしで出力する - Qiita

という記事が出て来た。

util.print

sys モジュールは deprecated なんで、util モジュールを使いましょう。

なるほど、 util なんてもんがあるのか、と思って使ってみる。

const util = require('util')

util.print('hoge')
util.print('hoge')

# hogehoge(node:67917) [DEP0026] DeprecationWarning: util.print is deprecated. Use console.log instead.

hogehoge って出たけど Warning も出ている。

The util.print() API is deprecated. Please use console.log() instead. Deprecated APIs | Node.js v9.2.0 Documentation

なるほど util.print() は deprecated だから console.log() を使えと...。

process.stdout.write

process.stdout.write('hoge')
process.stdout.write('hoge')

# hogehoge

これで良さそう。

Node.jsのconsole.logとprocess.stdout.writeの違い - Tatsuya Oiwa を読むに、 console.log は process.stdout.write のラッパーっぽいので信用して大丈夫そうだ。