コンテンツにスキップ

Chalk to Node.js util styleText

R

richiemccoll

Migrate from Chalk to Node.js util styleText

chalk-to-util-styletext

This codemod aims to help you reduce external dependencies by transforming chalk method calls to use the native Node.js styling functionality. It will also handle automatic removal of the chalk package from the package.json.

Compatible Features:

  • Basic colors (red, green, blue, yellow, etc.)
  • Bright colors (redBright, greenBright, etc.)
  • Background colors (bgRed, bgGreen, etc.)
  • Text modifiers (bold, dim, italic, underline, strikethrough, etc.)
  • Style chaining via array syntax
  • Environment variable support (NO_COLOR, NODE_DISABLE_COLORS, FORCE_COLOR)

Non-Compatible Features:

  • Custom RGB colors (chalk.rgb(), chalk.hex())
  • 256-color palette (chalk.ansi256())
  • Template literal syntax (chalk...``)
  • Advanced modifiers with limited terminal support (overline, blink, etc.)

Prerequisites:

Node.js Version Requirements

  • Node.js v20.12.0 or later (for util.styleText)
  • util.styleText became stable in Node.js v22.13.0 (and v23.5.0)

If your package supports a version earlier than 22, this creates a breaking change, which means you must bump the major version of your package.

Usage:

The source code for this codemod can be found in the chalk-to-util-styletext directory.

You can find this codemod in the Codemod Registry.

npx codemod @nodejs/chalk-to-util-styletext

Example:

import  from 'chalk';

.(.red('Error message'));

.(.red.bold('Important error'));

const  = .red;
.(('Error'));

const  = .blue.bold;
.(('Info'));

Recognition

We would like to thank the maintainers of chalk for their support of the package over time and for its contributions to the ecosystem.