Migrates usage of the chalk npm package to the Node.js built-in util.styleText() API. Replaces the chalk import with { styleText } from node:util and rewrites all chalk method calls accordingly. Chained chalk styles are converted to an array of style strings.
Run this codemod with:
npx codemod @nodejs/chalk-to-util-styletext
Basic color methods (ESM default import)
-import chalk from "chalk"; +import { styleText } from "node:util"; -console.log(chalk.red("Error message")); -console.log(chalk.green("Success message")); -console.log(chalk.blue("Info message")); +console.log(styleText("red", "Error message")); +console.log(styleText("green", "Success message")); +console.log(styleText("blue", "Info message"));
Chained styles
-import chalk from "chalk"; +import { styleText } from "node:util"; -console.log(chalk.red.bold("Error: Operation failed")); -console.log(chalk.green.underline("Success: All tests passed")); -console.log(chalk.yellow.bgBlack("Warning: Deprecated API usage")); +console.log(styleText(["red", "bold"], "Error: Operation failed")); +console.log(styleText(["green", "underline"], "Success: All tests passed")); +console.log(styleText(["yellow", "bgBlack"], "Warning: Deprecated API usage"));
CommonJS require
-const chalk = require("chalk"); +const { styleText } = require("node:util"); -const error = chalk.red("Error"); -const warning = chalk.yellow("Warning"); -const info = chalk.blue("Info"); +const error = styleText("red", "Error"); +const warning = styleText("yellow", "Warning"); +const info = styleText("blue", "Info"); console.log(error, warning, info);
Chalk methods that have no direct util.styleText equivalent — including hex(), rgb(), ansi256(), bgAnsi256(), visible(), and new chalk.Chalk() — are skipped. A warning is printed for each unsupported call, and those call sites are left unchanged for manual review.