Node.js Chalk Color Example (2024) | TechGeekNext

Node.js Chalk Color Example (2024)

In this tutorial, we will learn how to use Chalk utility package to color the text in node.js application.

Q: What is chalk in node js?
Ans:

The chalk module is an optional third-party library for text styling. In a Node.js project, it helps to design their own themes. This module allows users to personalize response messages by using different colors based on their choices.

Import NPM modules

  1. Create new folder for the project and open it in Visual Studio Editor.
  2. You can install Visual Studio Editor.
  3. Node.js Visual Studio Editor
  4. Create new file student-info.js under the project or while executing below npm command will point to default file called index.js
  5. The npm init command will install or create the package.json file if you run it as follows.
    D:\nodejs\chalk-example>npm init
    This utility will walk you through creating a package.json file.
    It only covers the most common items, and tries to guess sensible defaults.
    
    See `npm help init` for definitive documentation on these fields
    and exactly what they do.
    
    Use `npm install <pkg>` afterwards to install a package and
    save it as a dependency in the package.json file.
    
    Press ^C at any time to quit.
    package name: (chalk-example)
    version: (1.0.0)
    description: chalk color example
    entry point: (student-info.js)
    test command:
    git repository:
    keywords:
    author: techgeeknext
    license: (ISC)
    About to write to D:\nodejs\chalk-example\package.json:
    
    {
      "name": "chalk-example",
      "version": "1.0.0",
      "description": "chalk color example",
      "main": "student-info.js",
      "scripts": {
        "test": "echo \"Error: no test specified\" && exit 1"
      },
      "author": "techgeeknext",
      "license": "ISC"
    }
    
    
    Is this OK? (yes) yes
    npm notice
    npm notice New major version of npm available! 7.13.0 -> 8.12.1
    npm notice Changelog: https://github.com/npm/cli/releases/tag/v8.12.1
    npm notice Run npm install -g npm@8.12.1 to update!
    npm notice

Project Structure

Node.js Chalk Color Example

Install Chalk dependencies

  1. Go to https://www.npmjs.com/ and search Chalk.
  2. You can see it'll navigate to Chalk Page, where you can find the command to install chalk dependencies and if you scroll down further you can see more details how to use the Chalk in Node js. Node.js Chalk Color Example
  3. Now execute npm i chalk command to install all it's dependencies modules.
    D:\nodejs\chalk-example>npm i chalk
    npm WARN registry Using stale data from https://registry.npmjs.org/ because the host is inaccessible -- are you offline?
    npm WARN registry Using stale data from https://registry.npmjs.org/ due to a request error during revalidation.
    
    added 6 packages, and audited 7 packages in 14s
    
    2 packages are looking for funding
      run `npm fund` for details
    
    found 0 vulnerabilities
  4. package.json will be updated with Chalk dependency.
    {
        "name": "chalk-example",
        "version": "1.0.0",
        "description": "chalk color example",
        "main": "student-info.js",
        "scripts": {
            "test": "echo \"Error: no test specified\" && exit 1"
        },
        "author": "techgeeknext",
        "license": "ISC",
        "dependencies": {
            "chalk": "^4.1.2"
        }
    }
    

Node.js Chalk

Import the chalk dependency in the student-info.js file. And we will be able to use it's functions for coloring the text.

const chalk = require('chalk')

console.log(chalk.white.bgRed.bold('Welcome to TechGeekNext!'));

console.log(chalk.green.inverse.bold('Welcome to TechGeekNext!'));

Run the Node.js Chalk Example

Execute the node student-info.js command in the terminal to view the output for color being applied on the text. Node.js Chalk Color Example

Recommendation for Top Popular Post :