How to add color to your console or terminal
In this article, I will demonstrate how to add colors to your console or terminal in your NodeJS application.
Check out my previous post on how to create a basic nodeJS API that serves content in JSON. PREVIOUS POST
First, create a new folder and give it a name of your choice.
Open the command line or Terminal and move to the new folder directory just created.
Run the following code
npm init
Follow the steps as shown and change the entry name to server.js
Open Folder in any code editor of your choice. I use vs code. Open package.json file and add this code snippet in scripts below test put a comma and add
"start": "node server.js"
package.json
{
"name": "colorarticle",
"version": "1.0.0",
"description": "COlor Article",
"main": "server.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node server"
},
"author": "Pascal Ojinnaka",
"license": "ISC",
"dependencies": {
"colors": "^1.4.0",
"express": "^4.17.1"
}
}
Installing Express and colors package
Open folder directory on your command prompt or Terminal and type this command
npm install express colors
Create a new file server.js import express and colors then create a server with the code below
const express = require('express')
const colors = require('colors')
const app = express()
const PORT = 5000
app.listen(PORT,
console.log(`Server running on port ${PORT}`.yellow.inverse))
console.log('Hello !'.green.inverse)
when you get "Server running on port 5000" on your console with a yellow inverse color, congratulations your application is working.
NOTE: color can be changed to suit your type of console response. check out documentation
Thank you for reading