Node.js – Chalk 模块
chalk 模块是一个第三方库,可用于给文本添加样式。它允许用户在 Node.js 项目中创建自己的主题。
此模块可帮助用户使用不同的颜色自定义响应消息,以满足自己的喜好。
它还通过提供颜色来提高可读性,并使检测警告和错误变得更加容易。
安装
npm install chalk
示例 1
创建一个名为“chalk.js”的文件,并复制以下代码。创建文件后,使用命令“node chalk.js”来运行此代码,如下例所示 −
// Importing the chalk module
const chalk=require("chalk");
// Coloring different text messages
console.log(chalk.green("Welcome to Tutorials Point"))
console.log(chalk.red.underline("Welcome to Tutorials Point"))
console.log(chalk.red.underline.bold("Welcome to Tutorials Point"))输出

示例 2
我们再举一个例子
// Importing the chalk module
const chalk=require("chalk");
// Coloring different text messages
const welcome=chalk.green;
const warning=chalk.red;
console.log(welcome("Welcome to Tutorials Point"))
console.log(welcome("Success !!!"))
console.log(warning("Error - An unknown error occurred !"))
console.log(warning("Warning - Exception occurred !"))输出

广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP