|
| 1 | +--- |
| 2 | +date: '2026-01-23T00: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`](https://github.com/chalk/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 | +### Incompatible 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 | +### Prerequisites: |
| 32 | + |
| 33 | +#### Node.js Version Requirements |
| 34 | + |
| 35 | +- Node.js v20.12.0 or later (for util.styleText) |
| 36 | +- `util.styleText` became stable in Node.js v22.13.0 (and v23.5.0) |
| 37 | + |
| 38 | +> If your package currently supports Node.js versions earlier than v20.12.0, you cannot migrate to util.styleText without dropping support for those versions. |
| 39 | +> This requires bumping the major version of your package AND updating the engines field in your package.json to require Node.js >= v20.12.0. |
| 40 | +
|
| 41 | +### Usage: |
| 42 | + |
| 43 | +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). |
| 44 | + |
| 45 | +You can find this codemod in the [Codemod Registry](https://app.codemod.com/registry/@nodejs/chalk-to-util-styletext). |
| 46 | + |
| 47 | +```bash |
| 48 | +npx codemod @nodejs/chalk-to-util-styletext |
| 49 | +``` |
| 50 | + |
| 51 | +### Example: |
| 52 | + |
| 53 | +```js displayName="Before" |
| 54 | +import chalk from 'chalk'; |
| 55 | + |
| 56 | +console.log(chalk.red('Error message')); |
| 57 | + |
| 58 | +console.log(chalk.red.bold('Important error')); |
| 59 | + |
| 60 | +const red = chalk.red; |
| 61 | +console.log(red('Error')); |
| 62 | + |
| 63 | +const boldBlue = chalk.blue.bold; |
| 64 | +console.log(boldBlue('Info')); |
| 65 | +``` |
| 66 | + |
| 67 | +```js displayName="After" |
| 68 | +import { styleText } from 'node:util'; |
| 69 | + |
| 70 | +console.log(styleText('red', 'Error message')); |
| 71 | + |
| 72 | +console.log(styleText(['red', 'bold'], 'Important error')); |
| 73 | + |
| 74 | +const red = text => styleText('red', text); |
| 75 | +console.log(red('Error')); |
| 76 | + |
| 77 | +const boldBlue = text => styleText(['blue', 'bold'], text); |
| 78 | +console.log(boldBlue('Info')); |
| 79 | +``` |
| 80 | + |
| 81 | +## Recognition |
| 82 | + |
| 83 | +We would like to thank the maintainers of [`chalk`](https://github.com/chalk/chalk) for their support of the package over time and for its contributions to the ecosystem. |
0 commit comments