Get width of terminal in Node.js
node.jsTerminalnode.js Problem Overview
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).
node.js Solutions
Solution 1 - node.js
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.