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 !"))

輸出

更新於:2021年11月24日

2000+ 人次瀏覽

開啟你的職業生涯

完成課程獲得認證

開始學習
廣告