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

5

26

Introduction

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

Problem

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?

Used versions

  • Windows 7 SP1 64-bit
  • Git 2.5.3-32-bit with mintty 2.0.3
  • node 4.1.0 32-bit
  • npm 2.14.3
  • chalk 1.1.1

Update after more tests

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:

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

Herzen answered 23/9, 2015 at 14:53 Comment(2)
I get the same problem using the latest Git for Windows bundle from git-scm.org (2.6.0). It's MinTTY 2.0.3. I tried npm install -g chalk-cli and ran echo hello world | chalk bold cyan and the output was not colored :(Abrade
Just add export FORCE_COLOR=true to your .bashrc on Windows.Preliminaries
H
26

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

Heartbreaker answered 8/2, 2017 at 11:18 Comment(2)
Yep, that definitely works just drop it into .env and you've got colourUrushiol
.env I did not know but that would be per project right? In the windows user/system environment variables I meant in my initial answerHeartbreaker
S
13

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.

Spadiceous answered 26/10, 2015 at 10:45 Comment(2)
Too bad that there is no workaround. But thanks for the enlightenment!Herzen
Is this still true?Dominicdominica
B
10

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.

Brocky answered 10/1, 2019 at 17:50 Comment(1)
Works for WSL tooBirdsall
B
2

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.

Bigner answered 2/2, 2018 at 18:22 Comment(0)
T
1

I was successful with adding this to the node js file that was displaying the chalk output:

process.env.FORCE_COLOR = true
Tristis answered 21/8, 2020 at 21:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.