gpt4 book ai didi

node.js - 使用 chalk NPM 模块的终端样式(控制字符)

转载 作者:太空宇宙 更新时间:2023-11-04 00:16:27 24 4
gpt4 key购买 nike

假设我有这个:

//exp.js
const chalk = require('chalk');
console.log(chalk.red('foobar'));

然后我在命令行运行:

node exp.js | cat

在我见过的所有情况下,颜色都不会显示。有谁知道为什么?有没有办法让颜色显示出来?我做错了什么吗?

我唯一的猜测是 chalk当进程连接在管道中时,库“关闭”字符串样式?

例如:

enter image description here

最佳答案

根据 the fine manual :

$ node exp.js --color | cat
$ env FORCE_COLOR=1 node exp.js | cat

或者:

//exp.js
process.env.FORCE_COLOR = '1';
const chalk = require('chalk');
console.log(chalk.red('foobar'));

关于node.js - 使用 chalk NPM 模块的终端样式(控制字符),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46741639/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com