Getting Started
Installation and first API call
This page walks you through installing Chalk and making your first styled terminal output call. Chalk lets you chain color, background, and text decoration styles onto strings in a readable, expressive API — making CLI tools and logged output easier to scan at a glance. By the end of this tutorial, you will have Chalk installed, imported, and producing colored output in your Node.js application.
Before you begin, make sure you have the following:
- Node.js v14.x or later (Chalk v5+ is ESM-only; use Chalk v4 if you require CommonJS
require()syntax) - npm v6+ or yarn v1.22+ for package installation
- A JavaScript or TypeScript project already initialized (
package.jsonpresent) - Basic familiarity with the Node.js module system (ESM or CommonJS)
Get from zero to colored output in four steps:
- Install Chalk into your project.
- Import Chalk in your script.
- Call a style method on a string.
- Run your script and verify the styled output in your terminal.
Step 1 — Install Chalk
Run the following command in your project root:
npm install chalk
Or with yarn:
yarn add chalk
Success looks like: chalk appears under dependencies in your package.json and is present in node_modules/chalk.
Step 2 — Import Chalk
Chalk v5+ is a pure ESM module, so you import it with the ES module import syntax. If your project uses CommonJS, either set "type": "module" in package.json, use a .mjs file extension, or install Chalk v4 and use require().
ESM (Chalk v5+):
import chalk from 'chalk';
CommonJS (Chalk v4):
const chalk = require('chalk');
Success looks like: Your file parses without a module-related error when you run it.
Step 3 — Apply your first style
Call a style property on the chalk object and pass your string as the argument:
console.log(chalk.green('Hello, Chalk!'));
Styles are accessed as chainable properties. Each property wraps the string with the appropriate ANSI escape codes before it reaches the terminal.
Success looks like: Your script runs without errors and proceeds to the next step.
Step 4 — Run the script
node index.mjs
Success looks like: The text Hello, Chalk! appears in your terminal in green. If your terminal does not render color, see the Troubleshooting section.
Basic color
Apply a single foreground color to a string.
import chalk from 'chalk';
console.log(chalk.blue('Connecting to server...'));
console.log(chalk.red('Error: connection refused'));
console.log(chalk.yellow('Warning: retrying in 5s'));
Expected output: Each line prints in its respective color — blue, red, and yellow — in a color-capable terminal.
Chaining styles
Chain multiple style properties together to combine colors, backgrounds, and text decorations. Each chained property narrows the style applied to the final string argument.
import chalk from 'chalk';
console.log(chalk.bold.white.bgRed(' FATAL ') + chalk.red(' Unhandled exception'));
console.log(chalk.underline.cyan('https://example.com'));
Expected output: The first line prints FATAL as bold white text on a red background, followed by the error message in red. The second line prints the URL underlined in cyan.
Template literals with tagged strings
Use Chalk inside template literals to mix styled and unstyled text in a single line.
import chalk from 'chalk';
const user = 'Alice';
const status = 'online';
console.log(`User ${chalk.green(user)} is currently ${chalk.bold(status)}.`);
Expected output: The username appears in green, the status word appears in bold, and the surrounding text remains in the default terminal color.
256-color and Truecolor
For terminals that support extended colors, use chalk.ansi256() for 256-color mode or chalk.rgb() for full Truecolor.
import chalk from 'chalk';
// 256-color by index
console.log(chalk.ansi256(208)('Orange-ish text'));
// Truecolor by RGB values
console.log(chalk.rgb(255, 136, 0)('True orange text'));
console.log(chalk.bgRgb(0, 95, 184).white(' Branded background '));
Expected output: Text styled with the specified color values in terminals that support Truecolor (most modern terminal emulators). In terminals with limited support, Chalk automatically downgrades to the nearest supported color.
No color appears — output is plain text
Symptom: Your script runs without errors but text appears in the default terminal color with no styling.
Likely cause: Chalk detects the color support level of your terminal automatically. If it detects no color support (e.g., a CI environment or a terminal with TERM=dumb), it disables all styling to avoid printing raw ANSI escape codes.
Fix: Force a color level by setting the FORCE_COLOR environment variable before running your script:
FORCE_COLOR=1 node index.mjs
Or set the level programmatically in your code using chalk.level (values: 0 = no color, 1 = basic, 2 = 256 colors, 3 = Truecolor):
import chalk, { Chalk } from 'chalk';
const customChalk = new Chalk({ level: 3 });
console.log(customChalk.green('Forced Truecolor'));
require() of ES Module error
Symptom: You see an error like Error [ERR_REQUIRE_ESM]: require() of ES Module ... not supported.
Likely cause: You are using require('chalk') in a CommonJS file, but Chalk v5+ is ESM-only and cannot be loaded with require().
Fix (option A): Switch to ESM by adding "type": "module" to your package.json and using import chalk from 'chalk'.
Fix (option B): Downgrade to Chalk v4, which supports CommonJS:
npm install chalk@4
Then use:
const chalk = require('chalk');
Styles appear as raw escape codes in logs or files
Symptom: Output such as \u001B[32mHello\u001B[39m appears instead of colored text when you write output to a file or pipe it to another process.
Likely cause: Chalk is enabled even though the output destination does not support ANSI codes. Chalk normally auto-detects this, but FORCE_COLOR or a manual chalk.level setting may be overriding detection.
Fix: Do not force color when piping output or writing to log files. Remove FORCE_COLOR from your environment or conditionally set chalk.level = 0 when your application detects non-TTY output:
import chalk, { Chalk } from 'chalk';
import process from 'process';
const safeChalk = new Chalk({ level: process.stdout.isTTY ? 3 : 0 });
console.log(safeChalk.green('Only styled in a real terminal'));
Cannot find package 'chalk'
Symptom: Node throws Error: Cannot find package 'chalk' at runtime.
Likely cause: Chalk was not installed, the installation failed silently, or you are running the script from a directory where node_modules is not accessible.
Fix: Re-run the install command from the same directory that contains your package.json:
npm install chalk
Confirm the installation succeeded by checking that node_modules/chalk exists.