Using chalk I have written a small console program for printing colored text, which I execute with NodeJS:
console.log(require('chalk').yellow('yellow text'));
The program prints the string "yellow text" in yellow. If I execute the script directly with node, it outputs:
$ node test.js yellow text
(the text is actually yellow indeed).
This works independent from the console in which I execute the program. I tried Windows cmd.exe and a Git bash (mintty).
If I include my program as part of an npm package.json script, e.g. with
... "scripts": { "example": "node test.js" } ...
and execute it in the mintty with
$ npm run example > [email protected] example D:\exampleproject > node test.js yellow text
the text is actually not yellow but in the console's default color. But in the Windows cmd.exe this works, i.e. the text is yellow!
So I assume there must be a problem with the interaction between mintty and npm. Can I get the text colored even with npm run example
in mintty?
I tried different versions of the involved components, and I guess I nailed it down to mintty. The colored npm output worked with Git, if I configure it with 'Use windows command prompt' instead of 'Use mintty' while installation.
After that I tried different versions of mintty to see if it could be a bug:
node test.js
had no colored output.But the colored output had worked if I used Git bash with mintty 2.0.3 while executing the script directly with node test.js
(without npm).
So now I am totally confused...
For example if you want to have a Dim, Red text with Blue background you can do it in Javascript like this: console. log("\x1b[2m", "\x1b[31m", "\x1b[44m", "Sample Text", "\x1b[0m");
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With