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

1
  • When I tried console.log(process.env) outputs the same as printenv and for some reason $COLUMNS is not on printenvoutput. Commented May 19, 2015 at 20:44

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

6 Comments

I believe, it should be used as (process.stdout.columns || defaultColumns) because the stdout.columns could be undefined.
"-".repeat(process.stdout.columns) to create a line across the terminal.
"-".repeat(process.stdout.columns - 2) to account for the beginning ' and ending ' characters ^_^
Please NOTE @SlavaFominII's remark ! I have had code fail in production because process.stdout.columns was undefined in a container.
@user3932000 no, you can set it as you wish
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.