Add colored and formatted CLI output
✓Works with OpenClaudeYou are a Node.js CLI developer. The user wants to add colored and formatted text output to command-line applications using industry-standard libraries.
What to check first
- Run
npm list chalkto verify the chalk library is installed (ornpm install chalk) - Confirm your Node.js version supports ES modules or CommonJS (Node 12+)
Steps
- Import chalk at the top of your CLI file using
const chalk = require('chalk');for CommonJS orimport chalk from 'chalk';for ES modules - Use
chalk.red(),chalk.blue(),chalk.green()to apply foreground colors to strings - Chain
.bold,.italic,.underlinefor text styling before the color method or after it - Use
chalk.bgRed(),chalk.bgBlue()for background colors with 3-digit hex support likechalk.bgHex('#FF0000') - Combine multiple styles with method chaining:
chalk.bold.red.bgWhite() - Use template literals to interpolate colored output:
console.log(`Status: ${chalk.green('✓ Ready')}`) - Apply colors conditionally based on environment: check
process.env.NO_COLORto disable colors when needed - Test output with
node yourfile.jsand verify colors display correctly in your terminal
Code
const chalk = require('chalk');
// Basic color output
console.log(chalk.blue('This is blue text'));
console.log(chalk.green.bold('Success message'));
console.log(chalk.red.underline('Error message'));
// Background colors
console.log(chalk.bgYellow.black('Warning!'));
console.log(chalk.bgGreen.white('All clear'));
// Hex and RGB colors
console.log(chalk.hex('#FF6B6B')('Custom red'));
console.log(chalk.rgb(100, 200, 255)('Custom blue'));
// Combining styles
console.log(chalk.bold.italic.cyan('Important info'));
// Template literals with colors
const status = 'complete';
const result = `Task: ${chalk.green('✓')} ${chalk.bold(status)}`;
console.log(result);
// Conditional coloring
function log(message, type = 'info') {
const styles = {
success: chalk.green,
error: chalk.red,
warning: chalk.yellow,
info: chalk.blue,
};
const colorFunc = styles[type] || chalk.white;
console.log(colorFunc(message));
}
log('Operation successful', 'success');
log('Something went wrong', 'error');
log('Be careful', 'warning');
// Respecting NO_COLOR environment variable
const output = process.env.NO_COLOR ? chalk.stripColor : chalk;
console.log(output.green('This respects NO_COLOR flag'));
// Multi-
Note: this example was truncated in the source. See the GitHub repo for the latest full version.
Common Pitfalls
- Treating this skill as a one-shot solution — most workflows need iteration and verification
- Skipping the verification steps — you don't know it worked until you measure
- Applying this skill without understanding the underlying problem — read the related docs first
When NOT to Use This Skill
- When a simpler manual approach would take less than 10 minutes
- On critical production systems without testing in staging first
- When you don't have permission or authorization to make these changes
How to Verify It Worked
- Run the verification steps documented above
- Compare the output against your expected baseline
- Check logs for any warnings or errors — silent failures are the worst kind
Production Considerations
- Test in staging before deploying to production
- Have a rollback plan — every change should be reversible
- Monitor the affected systems for at least 24 hours after the change
Related CLI Tools Skills
Other Claude Code skills in the same category — free to download.
CLI App Builder
Build CLI applications with Commander.js
Interactive CLI
Create interactive CLI prompts (Inquirer.js)
CLI Progress Bar
Add progress bars and spinners to CLI
CLI Config Manager
Build CLI configuration management
CLI Help Generator
Generate help text and man pages
CLI Testing
Test CLI applications
CLI Packaging
Package CLI for npm/brew distribution
CLI Autocomplete
Add shell autocompletion to CLI tools
Want a CLI Tools skill personalized to YOUR project?
This is a generic skill that works for everyone. Our AI can generate one tailored to your exact tech stack, naming conventions, folder structure, and coding patterns — with 3x more detail.