nodejs-cli-guide/03_chalk at main · KunalSambyal/nodejs-cli-guide · GitHub
Skip to content

Latest commit

 

History

History

03_chalk

Folders and files

README.md

Chalk - Complete Guide

Chalk is a popular Node.js library for terminal string styling. It enables terminal applications to format output with colors, bold text, background highlights, and custom Hex/RGB color schemes.


1. Installation

Install Chalk in your Node.js project:

npm install chalk

If using TypeScript and tsx as your runner:

npm install -D typescript tsx @types/node

2. Core Concepts & Features

1. Basic Text Colors

Chalk provides direct color methods for standard terminal colors:

import chalk from "chalk";

console.log(chalk.green("Operation successful"));
console.log(chalk.red("Error occurred"));
console.log(chalk.yellow("Warning issue"));
console.log(chalk.blue("Informational message"));
console.log(chalk.gray("Debug logs"));

2. Style Chaining & Modifiers

Combine color methods with text modifiers like bold, dim, italic, underline, and strikethrough:

console.log(chalk.bold.green("Bold Green Success"));
console.log(chalk.underline.blue("Underlined Blue Link"));
console.log(chalk.bold.red.underline("Critical System Failure"));

3. Background Colors

Apply background colors using the bg prefix:

console.log(chalk.bgRed.white(" ERROR "));
console.log(chalk.bgGreen.black(" SUCCESS "));
console.log(chalk.bgYellow.black(" WARNING "));

4. Custom Hex and RGB Colors

Specify exact color codes matching your application branding:

// Custom Hex Colors
console.log(chalk.hex("#FFA500")("Orange text output"));
console.log(chalk.hex("#00FF88").bold("Neon green header"));

// RGB Colors
console.log(chalk.rgb(255, 136, 0)("Custom RGB orange"));

5. Nested Styling & Template Literals

Style specific variables inside longer strings without affecting surrounding text:

const movie = "Interstellar";

// Only the variable is styled
console.log(`Now playing: ${chalk.cyan.bold(movie)} in theaters.`);

// Nested styling inside a colored block
console.log(
  chalk.green(`Successfully added ${chalk.bold(movie)} to your watchlist.`)
);

6. Reusable Theme Helpers

Define reusable style functions to maintain consistent coloring across your codebase:

import chalk from "chalk";

// Define project color theme
export const theme = {
  success: chalk.bold.green,
  error: chalk.bold.red,
  warning: chalk.bold.yellow,
  info: chalk.cyan,
  subtle: chalk.gray
};

// Usage across services
console.log(theme.success("Data saved successfully"));
console.log(theme.error("File not found"));

3. Standard CLI Color Conventions

Role Color Standard Example Use Case
Success chalk.green Action completed, file saved, process finished
Error chalk.red Failures, missing arguments, unhandled exceptions
Warning chalk.yellow Deprecations, missing optional configuration
Information chalk.blue / chalk.cyan System status, headers, interactive prompt titles
Subtle / Debug chalk.gray Timestamps, file paths, verbose log lines