在 Node.js 中設定控制檯的字型顏色和樣式
你可以使用 chalk 模組 來定製控制檯上列印的語句。透過使用它,可以將文字字型顏色更改為任何顏色。還可以將字型樣式更改為粗體、斜體或下劃線。此外,你可以突出顯示列印的文字。
語法
npm install chalk
示例 1
建立檔案 "color.js" 並複製以下程式碼片段。建立檔案後,使用命令 "node color.js" 執行此程式碼。
// Giving color to console statments
// Importing the chalk module
const chalk = require('chalk');
// Printing the text in blue color
console.log(chalk.blue('Welcome to Tutorials Point !'));
// Printing the text in red color
console.log(chalk.red('Error occured !!! '));
// If color is not present we can pass the
// RGB code to give color
console.log(chalk.rgb(100, 150, 70)
('Custom statement - Hi...'));輸出
C:\home
ode>> node color.js

示例 2
// Giving color to console statments
// Importing the chalk module
const chalk = require('chalk');
// Setting background color to red
console.log(chalk.bgGreen('Welcome to Tutorials Point'));
// Setting background color to Bright Black
console.log(chalk.bgBlackBright('Hello User !!! '));
// Setting background color to Bright Red
console.log(chalk.bgRedBright('Hello... I am a custom color.'));輸出
C:\home
ode>> node color.js

示例 3
// Giving color to console statments
// Importing the chalk module
const chalk = require('chalk');
// Setting the text style to Bold
console.log(chalk.bold('Welcome to Tutorials Point in bold'));
// Setting the text style to dim
console.log(chalk.dim('Welcome to Tutorials Point in Dim'));
// Setting the text style to Italic
console.log(chalk.italic('Welcome to Tutorials Point in Italic'));
// Setting the text style to Bold
console.log(chalk.underline('Welcome to Tutorials Point in Underline'));輸出
C:\home
ode>> node color.js

廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP