1

I want to style Console output (console.log('%cTest', 'color: lime')) for debug purposes so I need to tell if script is running inside VSCode or not. Is it possible?

Clarification: I want "isomorphic" code which can detect if it's running inside VSCode and run colored console.log output or not in VSCode and run regular console.log.

2
  • You can run node yourfilename.js inside the terminal which comes with vscode if that's what you are asking. Your question is unclear. Commented Jul 7, 2019 at 16:30
  • @muhammad-kamran I want "isomorphic" code which can detect if it's running inside VSCode and run colored console.log output or not in VSCode and run regular console.log. Commented Jul 7, 2019 at 23:19

1 Answer 1

2

Actually, what you need is detecting whether the console support color (and how well it supports color), not sniff whether the Node.js program is launched from VSCode.

To implement this requirement, npm module supports-color can be used:

// Code example taken from supports-color document

const supportsColor = require('supports-color');

if (supportsColor.stdout) {
    console.log('Terminal stdout supports color');
}

if (supportsColor.stdout.has256) {
    console.log('Terminal stdout supports 256 colors');
}

if (supportsColor.stderr.has16m) {
    console.log('Terminal stderr supports 16 million colors (truecolor)');
}
Sign up to request clarification or add additional context in comments.

3 Comments

Actually, the code inside this module just check the platform and environment to figure out the support, not actually checking it.
Well, the supports-color module will check process.env (environment variables for current process), such as FORCE_COLOR or COLORTERM etc. I think it is the right thing to do (check current process details). The module does not sniff specific platform.
TBH, it does check if win platform: github.com/chalk/supports-color/blob/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.