@colors/colors

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

在节点.js控制台中获得颜色

中文使用文档
文档目录17 节
  1. 分发和维修
  2. 在节点.js控制台中获得颜色和样式
  3. 安装
  4. 颜色和风格!
  5. 文本颜色
  6. 亮文本颜色
  7. 背景颜色
  8. 亮背景颜色
  9. 样式
  10. 额外
  11. 使用量
  12. 启用/ 禁用颜色
  13. 控制台.log 字符串替换
  14. 自定义主题
  15. 使用标准 API
  16. 使用字符串安全 API
  17. 组合颜色

@ 颜色/ 颜色 ("colors.js")

分发和维修

达格·哈马舍尔德图书馆 分配和维护 DABH 有限责任公司。 。 。 . 维修和软件咨询查询: 哈啰@dabh.llc。 。 。 .

这个发行基于原色. js库由Marak Squires和 sindre Sorhus和DABH的额外功能。 版权通知原件 并保留麻省理工学院的许可证 自由主义。 。 。 .

Build Status version

请检查一下 路线图 为即将到来的特性和发布。 请打开问题来提供反馈 .

在节点.js控制台中获得颜色和样式

Demo

安装

npm install @colors/colors

颜色和风格!

文本颜色

  • 黑色
  • 红色
  • 绿色
  • 黄色
  • 蓝色
  • 红色
  • 青红
  • 白色
  • 灰色
  • 灰色

亮文本颜色

  • 亮红色
  • 亮绿色
  • 亮黄色
  • 亮蓝色
  • 亮红色
  • 闪亮的青色
  • 发光白

背景颜色

  • bg 布莱克
  • bg红
  • bg 绿色
  • bg黄色
  • bg 蓝色
  • bg 红色
  • bg 青色线
  • bg 白调
  • bgGray (英语)
  • bg格雷语

亮背景颜色

  • bg 亮红色
  • bg 淡绿色
  • bg 亮黄色
  • bg 淡蓝色
  • bg Bright 红外线
  • bg 闪亮青色
  • bg 右白线

样式

  • 重设
  • 粗体
  • 阴暗
  • 斜体
  • 下划线
  • 反转
  • 隐藏
  • 击穿

额外

  • 彩虹
  • 斑马
  • 美国
  • 陷阱
  • 随机

使用量

根据大众的要求, @colors/colors 现在船有两种用途!

超级美妙的方式

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

或稍微小一点的不延伸的方式 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

我更喜欢第一种方式。 有些人似乎害怕扩展 String.prototype 更喜欢第二种方式.

如果你写好的代码 你永远不会有 与第一个方法的问题。 如果你真的不想碰的话 String.prototype,第二个用法不会触碰 String 原生对象.

启用/ 禁用颜色

软件包将自动检测您的终端是否可以使用颜色并相应启用/失效 。 当颜色被禁用时,颜色函数无所作为. 您可以使用命令行旗来覆盖它 :

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

或用代码 :

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

控制台.log 字符串替换

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

自定义主题

使用标准 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);

使用字符串安全 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"));

组合颜色

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

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

console.log('test'.custom);

Protip : (英语) 里面有个秘密的无证风格 colors如果你找到那种风格 你可以召唤他.

版本与发布记录

@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 是什么?

在节点.js控制台中获得颜色

如何安装 @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 和提交记录判断。