This recipe migrates from the external chalk package to Node.js built-in util.styleText() API, eliminating an external dependency while maintaining terminal text styling functionality.
What It Does
The codemod transforms:
chalk.color('text') → styleText('color', 'text')
chalk.color.modifier('text') → styleText(['color', 'modifier'], 'text')
- Removes
chalk from package.json dependencies
- Updates both CommonJS and ES module imports
Usage
npx codemod nodejs/chalk-to-util-styletext
Examples
import chalk from 'chalk';
console.log(chalk.red('Error message'));
console.log(chalk.green('Success message'));
console.log(chalk.blue('Info message'));
import { styleText } from 'node:util';
console.log(styleText('red', 'Error message'));
console.log(styleText('green', 'Success message'));
console.log(styleText('blue', 'Info message'));
const chalk = require('chalk');
console.log(chalk.red('Error message'));
console.log(chalk.green('Success message'));
console.log(chalk.blue('Info message'));
const { styleText } = require('node:util');
console.log(styleText('red', 'Error message'));
console.log(styleText('green', 'Success message'));
console.log(styleText('blue', 'Info message'));
Chained Modifiers
For multiple styles applied to the same text:
import chalk from 'chalk';
console.log(chalk.red.bold('Important error'));
console.log(chalk.green.underline('Success with emphasis'));
import { styleText } from 'node:util';
console.log(styleText(['red', 'bold'], 'Important error'));
console.log(styleText(['green', 'underline'], 'Success with emphasis'));
Stored Style Functions
const chalk = require('chalk');
const red = chalk.red;
const boldBlue = chalk.blue.bold;
console.log(red('Error'));
console.log(boldBlue('Info'));
const { styleText } = require('node:util');
const red = (text) => styleText('red', text);
const boldBlue = (text) => styleText(['blue', 'bold'], text);
console.log(red('Error'));
console.log(boldBlue('Info'));
Compatibility
Supported Methods
The codemod supports most chalk methods:
- Colors:
black, red, green, yellow, blue, magenta, cyan, white, gray, grey
- Background colors:
bgBlack, bgRed, bgGreen, bgYellow, bgBlue, bgMagenta, bgCyan, bgWhite
- Text modifiers:
bold, dim, italic, underline, strikethrough
Unsupported Methods
The following chalk methods are not supported and will generate warnings:
hex() - Custom hex colors
rgb() - Custom RGB colors
ansi256() - 256-color codes
bgAnsi256() - Background 256-color codes
visible() - Conditional visibility
You’ll need to manually migrate these cases.
Automatic Cleanup
The codemod automatically removes the chalk dependency from your package.json after migration.
Limitations
- Complex conditional expressions in some contexts may need manual review
- Template literals with chalk are not automatically converted
- Custom chalk themes require manual migration
After running the codemod, run your tests to ensure all styling works as expected. The util.styleText() API has been available since Node.js v20.12.0.
Benefits of Migration
- Remove external dependency: One less package to maintain and update
- Native performance: Built-in APIs are optimized for Node.js
- Standard API: Aligned with Node.js core module conventions
- Smaller bundle: No need to include chalk in your dependencies