No console colors if using npm script inside a Git bash (mintty)

A workaround is to escape the coloured string, replace all the %1B by \u\u%1B and then, unescape it back.

I'm using chalk here, and it gets automatically disabled when running inside bash...but you can go around it and "force" it to get enabled

const chalk = require('chalk')
chalk.enabled = true
chalk.level = 3

function fixColors (str) {
  return unescape(
    escape(
      str
    )
    .replace(/\%1B/i, '\\u%1B')
  )
}

console.log(fixColors(chalk.blueBright('is it blue?!')))

I hope this helps someone :) It's ugly, but easy to implement.


The current work around seems to be (on windows) to set an environment variable:

FORCE_COLOR=true

src: Color support detection problem in git bash on windows


Creating the file ~/.bashrc with the content export FORCE_COLOR=true made colors work for me in Git Bash on Windows 10, as pointed out in slightly more general terms by diego nunes and leroyse.


It's related to the known problem on Node.js:

Node.js doesn't run as tty on windows / cygwin nodejs/node#3006

Git Bash Error - Cannot read property 'substring' #272.

Not sure, if it will ever be fixed.

In short, MSYS / Cygwin / etc. (using Mintty as terminal emulator) runs bash inside "fake" console, which doesn't get along with node. It will probably be the same for any other terminal emulator.

To check if Node.js is being run in a TTY context try this:

cd c:/nodejs
./node -p -e "Boolean(process.stdout.isTTY)"

Note, that simply running node -p -e "Boolean(process.stdout.isTTY)" won't do the trick in this case.