question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

chalk.enabled=true or { enabled: true} does not work when color support is initially detected as false

See original GitHub issue

Issuehunt badges

Environment

Chalk: 2.3.0 Node.js: 6.12.0 and 8.9.3

Issue

chalk.js

const chalk = require('chalk');
chalk.enabled = true;
console.log(chalk`{green hello}`);

–or–

const chalk = new require('chalk').constructor({ enabled: true });

If you run chalk.js as:

node chalk.js

The output is a green hello.

If you run this in a clean environment (which causes supports-color to report that colors are not supported):

env -i node chalk.js

The output is a un-colored hello.

Details

I traced this to https://github.com/chalk/chalk/blob/master/index.js#L177, where this.enabled is false, and this is builder. It seems like, somehow, the initial value for enabled is not getting changed or is not filtering down to the enabled property defined on builder.

As expected, using the environment variable FORCE_COLOR or the --color parameter does work, as it gets enabled to the right value, initially.

Interestingly, running it as (to force it on from the start):

env -i node chalk.js --color

…and then doing:

chalk.enabled = false;

does actually turn it off. Now I’m thinking there’s some issue with truthy and falsy. I’m not sure.

Aside

It would definitely be easier for me to dig more on this if there weren’t so much indirection in the way this is implemented 😇; but, I’m sure some or most of that is necessary to get this to work so cleanly.


IssueHunt Summary

qix- qix- has been rewarded.

Backers (Total: $40.00)

Submitted pull Requests


Tips


IssueHunt has been backed by the following sponsors. Become a sponsor

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:1
  • Comments:23 (17 by maintainers)

github_iconTop GitHub Comments

6reactions
mireflycommented, Jul 14, 2018

chalk.level is 0 when supports-color reports that colors are not supported. This makes output un-colored.

const chalk = require('chalk')                                                  
chalk.enabled = true                                                            
chalk.level = 3                                                                 
console.log(chalk`{green hello}`) 

outputs green text.

It seems not good to have un-colored output when chalk.enable is true , but I am not sure how to make it better.

1reaction
sindresorhuscommented, Dec 26, 2018

Yeah, I think we should remove .enabled for Chalk 3. PR welcome.

Read more comments on GitHub >

github_iconTop Results From Across the Web

No console colors if using npm script inside a Git bash (mintty)
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...
Read more >
Chalkboard Chalk Markers (10 Pack, 1mm Extra Fine Tip ...
Amazon.com : Chalkboard Chalk Markers (10 Pack, 1mm Extra Fine Tip) Neon color pens - For Blackboards, Chalkboard, Bistro, Window : Arts, Crafts...
Read more >
chalk - npm
Expressive API; Highly performant; Ability to nest styles; 256/Truecolor color support; Auto-detects color support; Doesn't extend String.
Read more >
Ask Irwin - Support & Services - IRWIN TOOLS
I just purchased an IRWIN chalk reel and I'm not sure which color chalk I should use for different applications. Choosing the appropriate...
Read more >
supports-color | Yarn - Package Manager
supports -color. owner chalk1.1bMIT9.3.1TS vulns 0 vulnerabilities. Detect whether a terminal supports color. color, colour, colors, terminal ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found