exebook
exebook

Reputation: 33900

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

Upvotes: 68

Views: 22951

Answers (1)

r0-
r0-

Reputation: 2498

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.

Upvotes: 117

Related Questions