@colors/colors

中文文档
1.6.1发布于 2026/09/07MITCommonJS零依赖近 30 天下载 3亿

get colors in your node.js console

原始使用文档
文档目录17 节
  1. Distribution and maintenance
  2. get color and style in your node.js console
  3. Installation
  4. colors and styles!
  5. text colors
  6. bright text colors
  7. background colors
  8. bright background colors
  9. styles
  10. extras
  11. Usage
  12. Enabling/Disabling Colors
  13. Console.log string substitution
  14. Custom themes
  15. Using standard API
  16. Using string safe API
  17. Combining Colors

@colors/colors ("colors.js")

Distribution and maintenance

DABH — Distributed and maintained by DABH LLC. For maintenance and software consulting inquiries: [email protected].

This distribution builds on the original colors.js library by Marak Squires and additional functionality by Sindre Sorhus and DABH. Original copyright notices and the MIT license are preserved in LICENSE.

Build Status version

Please check out the roadmap for upcoming features and releases. Please open Issues to provide feedback.

get color and style in your node.js console

Demo

Installation

npm install @colors/colors

colors and styles!

text colors

  • black
  • red
  • green
  • yellow
  • blue
  • magenta
  • cyan
  • white
  • gray
  • grey

bright text colors

  • brightRed
  • brightGreen
  • brightYellow
  • brightBlue
  • brightMagenta
  • brightCyan
  • brightWhite

background colors

  • bgBlack
  • bgRed
  • bgGreen
  • bgYellow
  • bgBlue
  • bgMagenta
  • bgCyan
  • bgWhite
  • bgGray
  • bgGrey

bright background colors

  • bgBrightRed
  • bgBrightGreen
  • bgBrightYellow
  • bgBrightBlue
  • bgBrightMagenta
  • bgBrightCyan
  • bgBrightWhite

styles

  • reset
  • bold
  • dim
  • italic
  • underline
  • inverse
  • hidden
  • strikethrough

extras

  • rainbow
  • zebra
  • america
  • trap
  • random

Usage

By popular demand, @colors/colors now ships with two types of usages!

The super nifty way

var colors = require('@colors/colors');

console.log('hello'.green); // outputs green text
console.log('i like cake and pies'.underline.red); // outputs red underlined text
console.log('inverse the color'.inverse); // inverses the color
console.log('OMG Rainbows!'.rainbow); // rainbow
console.log('Run the trap'.trap); // Drops the bass

or a slightly less nifty way which doesn't extend String.prototype

var colors = require('@colors/colors/safe');

console.log(colors.green('hello')); // outputs green text
console.log(colors.red.underline('i like cake and pies')); // outputs red underlined text
console.log(colors.inverse('inverse the color')); // inverses the color
console.log(colors.rainbow('OMG Rainbows!')); // rainbow
console.log(colors.trap('Run the trap')); // Drops the bass

I prefer the first way. Some people seem to be afraid of extending String.prototype and prefer the second way.

If you are writing good code you will never have an issue with the first approach. If you really don't want to touch String.prototype, the second usage will not touch String native object.

Enabling/Disabling Colors

The package will auto-detect whether your terminal can use colors and enable/disable accordingly. When colors are disabled, the color functions do nothing. You can override this with a command-line flag:

node myapp.js --no-color
node myapp.js --color=false

node myapp.js --color
node myapp.js --color=true
node myapp.js --color=always

FORCE_COLOR=1 node myapp.js

Or in code:

var colors = require('@colors/colors');
colors.enable();
colors.disable();

Console.log string substitution

var name = 'Beowulf';
console.log(colors.green('Hello %s'), name);
// outputs -> 'Hello Beowulf'

Custom themes

Using standard API


var colors = require('@colors/colors');

colors.setTheme({
  silly: 'rainbow',
  input: 'grey',
  verbose: 'cyan',
  prompt: 'grey',
  info: 'green',
  data: 'grey',
  help: 'cyan',
  warn: 'yellow',
  debug: 'blue',
  error: 'red'
});

// outputs red text
console.log("this is an error".error);

// outputs yellow text
console.log("this is a warning".warn);

Using string safe API

var colors = require('@colors/colors/safe');

// set single property
var error = colors.red;
error('this is red');

// set theme
colors.setTheme({
  silly: 'rainbow',
  input: 'grey',
  verbose: 'cyan',
  prompt: 'grey',
  info: 'green',
  data: 'grey',
  help: 'cyan',
  warn: 'yellow',
  debug: 'blue',
  error: 'red'
});

// outputs red text
console.log(colors.error("this is an error"));

// outputs yellow text
console.log(colors.warn("this is a warning"));

Combining Colors

var colors = require('@colors/colors');

colors.setTheme({
  custom: ['red', 'underline']
});

console.log('test'.custom);

Protip: There is a secret undocumented style in colors. If you find the style you can summon him.

版本与发布记录

@colors/colors 共发布 4 个版本,首次发布于 2022/02/12;过去 12 个月发布了 1 个版本。

版本发布日期距今
1.6.1latest2026/09/072 个月前
1.6.02023/07/103 年前
1.5.02022/02/124 年前
1.4.02022/02/124 年前

关于 @colors/colors 的常见问题

@colors/colors 是什么?

get colors in your node.js console

如何安装 @colors/colors?

在项目目录运行 npm install @colors/colors,或使用 pnpm add / yarn add / bun add。

@colors/colors 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/colors__colors 是否存在。

@colors/colors 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@colors/colors 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=0.1.90。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@colors/colors 可以商用吗?

@colors/colors 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@colors/colors 的下载量有多少?

根据 npm 官方下载统计,@colors/colors 在 2026/10/07 – 2026/11/05 这 30 天内被下载 297,618,353 次,日均约 9,920,612 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@colors/colors 还在维护吗?

最新版本 1.6.1 发布于 2026/09/07(2 个月前);至今共发布 4 个版本,其中过去 12 个月发布了 1 个;首次发布于 2022/02/12。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。