Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get width of terminal in Node.js

console.log(process.env.COLUMNS)

Yields undefined, although,

$ echo $COLUMNS

Outputs as expected:

78

I have been trying running Node like this env node myprog.js, still undefined. What's wrong with me or any other way to know the width of terminal? (For nice formatting of some output data).

like image 400
exebook Avatar asked May 19 '15 20:05

exebook


1 Answers

console.log('Terminal size: ' + process.stdout.columns + 'x' + process.stdout.rows);

And the output looks like e.g. "Terminal size: 80x24". There is also an event if the console size changes.

It's explained under tty in the docs.

like image 147
r0- Avatar answered Oct 23 '22 13:10

r0-