Get width of terminal in Node.js

node.jsTerminal

node.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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionexebookView Question on Stackoverflow
Solution 1 - node.jsr0-View Answer on Stackoverflow