When you run the script directly through node run.js process.stdout.columns works fine and returns the number of valid characters. When you run the script through a command created in package.json, npm run build , which runs essentially the same node run.js , process.stdout.columns returns undefined .

How to fix this and, most importantly, I want to know why this is happening?

  • one
    just checked the error you described, everything works for me, no matter how I run it, and npm or directly from node index.js and if npm runs the npm command node -v v6.3.1 - Vasily Barbashev
  • @ Vasily Barbashev is very strange - Vasya Shmarovoz

1 answer 1

Not exactly the same thing. Running through package.json actually executes the command via exec. Apparently it is invoked as a child process and it will not have a direct connection with the TTY so the stdio will not have TTY streams. There was a discussion on this topic https://github.com/nodejs/node/issues/2333