|
| 1 | +--- |
| 2 | +date: '2025-12-19T00:00:00.000Z' |
| 3 | +category: migrations |
| 4 | +title: Chalk to Node.js util styleText |
| 5 | +layout: blog-post |
| 6 | +author: richiemccoll |
| 7 | +--- |
| 8 | + |
| 9 | +# Migrate from Chalk to Node.js util styleText |
| 10 | + |
| 11 | +## `chalk-to-util-styletext` |
| 12 | + |
| 13 | +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. |
| 14 | + |
| 15 | +### Compatible Features: |
| 16 | + |
| 17 | +- Basic colors (red, green, blue, yellow, etc.) |
| 18 | +- Bright colors (redBright, greenBright, etc.) |
| 19 | +- Background colors (bgRed, bgGreen, etc.) |
| 20 | +- Text modifiers (bold, dim, italic, underline, strikethrough, etc.) |
| 21 | +- Style chaining via array syntax |
| 22 | +- Environment variable support (NO_COLOR, NODE_DISABLE_COLORS, FORCE_COLOR) |
| 23 | + |
| 24 | +### Non-Compatible Features: |
| 25 | + |
| 26 | +- Custom RGB colors (chalk.rgb(), chalk.hex()) |
| 27 | +- 256-color palette (chalk.ansi256()) |
| 28 | +- Template literal syntax (chalk...``) |
| 29 | + -Advanced modifiers with limited terminal support (overline, blink, etc.) |
| 30 | + |
| 31 | +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). |
| 32 | + |
| 33 | +You can find this codemod in the [Codemod Registry](https://app.codemod.com/registry/@nodejs/chalk-to-util-styletext). |
| 34 | + |
| 35 | +```bash |
| 36 | +npx codemod @nodejs/chalk-to-util-styletext |
| 37 | +``` |
| 38 | + |
| 39 | +### Example: |
| 40 | + |
| 41 | +```js displayName="Before" |
| 42 | +import chalk from 'chalk'; |
| 43 | + |
| 44 | +console.log(chalk.red('Error message')); |
| 45 | + |
| 46 | +console.log(chalk.red.bold('Important error')); |
| 47 | + |
| 48 | +const red = chalk.red; |
| 49 | +console.log(red('Error')); |
| 50 | + |
| 51 | +const boldBlue = chalk.blue.bold; |
| 52 | +console.log(boldBlue('Info')); |
| 53 | +``` |
| 54 | + |
| 55 | +```js displayName="After" |
| 56 | +import { styleText } from 'node:util'; |
| 57 | + |
| 58 | +console.log(styleText('red', 'Error message')); |
| 59 | + |
| 60 | +console.log(styleText(['red', 'bold'], 'Important error')); |
| 61 | + |
| 62 | +const red = text => styleText('red', text); |
| 63 | +console.log(red('Error')); |
| 64 | + |
| 65 | +const boldBlue = text => styleText(['blue', 'bold'], text); |
| 66 | +console.log(boldBlue('Info')); |
| 67 | +``` |
0 commit comments