> ## Documentation Index
> Fetch the complete documentation index at: https://mintlify.com/nodejs/userland-migrations/llms.txt
> Use this file to discover all available pages before exploring further.

# Chalk to util.styleText Migration

> Migrate from the external chalk package to Node.js built-in util.styleText() API for terminal text styling

This recipe migrates from the external `chalk` package to Node.js built-in `util.styleText()` API, eliminating an external dependency while maintaining terminal text styling functionality.

## What It Does

The codemod transforms:

* `chalk.color('text')` → `styleText('color', 'text')`
* `chalk.color.modifier('text')` → `styleText(['color', 'modifier'], 'text')`
* Removes `chalk` from package.json dependencies
* Updates both CommonJS and ES module imports

## Usage

```bash theme={null}
npx codemod nodejs/chalk-to-util-styletext
```

## Examples

### Basic Color Transformations

<Tabs>
  <Tab title="ESM">
    ```javascript Before theme={null}
    import chalk from 'chalk';
    console.log(chalk.red('Error message'));
    console.log(chalk.green('Success message'));
    console.log(chalk.blue('Info message'));
    ```

    ```javascript After theme={null}
    import { styleText } from 'node:util';
    console.log(styleText('red', 'Error message'));
    console.log(styleText('green', 'Success message'));
    console.log(styleText('blue', 'Info message'));
    ```
  </Tab>

  <Tab title="CommonJS">
    ```javascript Before theme={null}
    const chalk = require('chalk');
    console.log(chalk.red('Error message'));
    console.log(chalk.green('Success message'));
    console.log(chalk.blue('Info message'));
    ```

    ```javascript After theme={null}
    const { styleText } = require('node:util');
    console.log(styleText('red', 'Error message'));
    console.log(styleText('green', 'Success message'));
    console.log(styleText('blue', 'Info message'));
    ```
  </Tab>
</Tabs>

### Chained Modifiers

For multiple styles applied to the same text:

```javascript Before theme={null}
import chalk from 'chalk';
console.log(chalk.red.bold('Important error'));
console.log(chalk.green.underline('Success with emphasis'));
```

```javascript After theme={null}
import { styleText } from 'node:util';
console.log(styleText(['red', 'bold'], 'Important error'));
console.log(styleText(['green', 'underline'], 'Success with emphasis'));
```

### Stored Style Functions

```javascript Before theme={null}
const chalk = require('chalk');
const red = chalk.red;
const boldBlue = chalk.blue.bold;

console.log(red('Error'));
console.log(boldBlue('Info'));
```

```javascript After theme={null}
const { styleText } = require('node:util');
const red = (text) => styleText('red', text);
const boldBlue = (text) => styleText(['blue', 'bold'], text);

console.log(red('Error'));
console.log(boldBlue('Info'));
```

## Compatibility

### Supported Methods

The codemod supports most chalk methods:

* **Colors**: `black`, `red`, `green`, `yellow`, `blue`, `magenta`, `cyan`, `white`, `gray`, `grey`
* **Background colors**: `bgBlack`, `bgRed`, `bgGreen`, `bgYellow`, `bgBlue`, `bgMagenta`, `bgCyan`, `bgWhite`
* **Text modifiers**: `bold`, `dim`, `italic`, `underline`, `strikethrough`

### Unsupported Methods

<Warning>
  The following chalk methods are not supported and will generate warnings:

  * `hex()` - Custom hex colors
  * `rgb()` - Custom RGB colors
  * `ansi256()` - 256-color codes
  * `bgAnsi256()` - Background 256-color codes
  * `visible()` - Conditional visibility

  You'll need to manually migrate these cases.
</Warning>

### Automatic Cleanup

The codemod automatically removes the `chalk` dependency from your `package.json` after migration.

## Limitations

* Complex conditional expressions in some contexts may need manual review
* Template literals with chalk are not automatically converted
* Custom chalk themes require manual migration

<Tip>
  After running the codemod, run your tests to ensure all styling works as expected. The `util.styleText()` API has been available since Node.js v20.12.0.
</Tip>

## Benefits of Migration

* **Remove external dependency**: One less package to maintain and update
* **Native performance**: Built-in APIs are optimized for Node.js
* **Standard API**: Aligned with Node.js core module conventions
* **Smaller bundle**: No need to include chalk in your dependencies
