Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 67 additions & 0 deletions apps/site/pages/en/blog/migrations/chalk-to-styletext.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
---
date: '2025-12-19T00:00:00.000Z'
category: migrations
title: Chalk to Node.js util styleText
layout: blog-post
author: 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.)

The source code for this codemod can be found in the [chalk-to-util-styletext directory](https://github.com/nodejs/userland-migrations/tree/main/recipes/chalk-to-util-styletext).

You can find this codemod in the [Codemod Registry](https://app.codemod.com/registry/@nodejs/chalk-to-util-styletext).

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

### Example:

```js displayName="Before"
import chalk from 'chalk';

console.log(chalk.red('Error message'));

console.log(chalk.red.bold('Important error'));

const red = chalk.red;
console.log(red('Error'));

const boldBlue = chalk.blue.bold;
console.log(boldBlue('Info'));
```

```js displayName="After"
import { styleText } from 'node:util';

console.log(styleText('red', 'Error message'));

console.log(styleText(['red', 'bold'], 'Important error'));

const red = text => styleText('red', text);
console.log(red('Error'));

const boldBlue = text => styleText(['blue', 'bold'], text);
console.log(boldBlue('Info'));
```
Loading