-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
feat: add migration guide for chalk to util styletext codemod #8430
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
102ef04
f9d8180
4c08f37
6e7d37e
1d353e2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| --- | ||
| date: '2026-01-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`](https://github.com/chalk/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) | ||
|
|
||
AugustinMauroy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| > 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. | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Doesn't it mean they can't do it?
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. They can but it's not a good practice
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I mean if they migrate to a node API that doesn't exist in the older but "supported" version of node, it will blow up in that version. That's more than a breaking change—that's a broken change.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @JakobJingleheimer @AugustinMauroy what about something like this:
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. for me it's good idea |
||
|
|
||
| ### Usage: | ||
|
|
||
| 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')); | ||
| ``` | ||
AugustinMauroy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| ## Recognition | ||
|
|
||
| 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. | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
?