Creating a Node.js CLI and Publish

Written by
Written by
Akshay Gore
Akshay Gore
|
Software Engineer - I
Software Engineer - I
Published on
Published on
Technology

TL;DR
Create a fun Node.js CLI app by setting up a project folder, adding an index.js with a Node shebang, and using libraries like figlet, chalk, and chalk-animation to style and animate console output. Test it locally with node index.js, then update package.json, publish to npm, and run it globally via the command name you defined.
Introduction
Would you like to create a CLI (Command Line Interface) application with your friends? Follow these steps to get started:
Create a new directory for your CLI app
mkdir cli-app cd cli-app npm init
mkdir cli-app cd cli-app npm init
Install the necessary packages for beautiful text and colors
npm install chalk chalk-animation figlet
npm install chalk chalk-animation figlet
Create a new file named
index.jstouch index.jsAdd the following line at the top of the
index.jsfile
The line#!/usr/bin/env nodeat the beginning of a JavaScript file is called a shebang line. It instructs the system to
use the Node.js interpreter to run the script.
#!/usr/bin/env node
#!/usr/bin/env node
Create an Immediately Invoked Function Expression (IIFE) to begin the script. Let's start by using Figlet
(async () => { await figlet.text( "Hey There", { font: "Standard", horizontalLayout: "default", verticalLayout: "default", width: 70, whitespaceBreak: true, }, function (err, data) { if (err) { console.log("Something went wrong..."); console.dir(err); return; } console.log(data); } ); })(); // Figlet offers extensive documentation for further customization.
(async () => { await figlet.text( "Hey There", { font: "Standard", horizontalLayout: "default", verticalLayout: "default", width: 70, whitespaceBreak: true, }, function (err, data) { if (err) { console.log("Something went wrong..."); console.dir(err); return; } console.log(data); } ); })(); // Figlet offers extensive documentation for further customization.
Now, let's utilize chalk with simple
console.log()statements and template literals to customize the console logs
let Github = "https://github.com/Akshaygore1"; let Linkedin = "https://www.linkedin.com/in/akshaygore2301/"; let Portfolio = "https://akshaygore.tech"; let bio = `My name is ${chalk.bold.cyan( "Akshay Gore" )}. I am a Full Stack Developer proficient in ${chalk.yellow( "JavaScript" )} and ${chalk.blue("TypeScript")}.`; console.log(`\n${bio}`); console.log("\nYou Can Connect with me here:"); console.log(chalk.cyan("GitHub:"), Github); console.log(chalk.cyan("LinkedIn:"), Linkedin); console.log(chalk.cyan("Portfolio:"), Portfolio); // Here, I've added my socials links and used chalk to provide font colors. // You can also set background colors using chalk.
let Github = "https://github.com/Akshaygore1"; let Linkedin = "https://www.linkedin.com/in/akshaygore2301/"; let Portfolio = "https://akshaygore.tech"; let bio = `My name is ${chalk.bold.cyan( "Akshay Gore" )}. I am a Full Stack Developer proficient in ${chalk.yellow( "JavaScript" )} and ${chalk.blue("TypeScript")}.`; console.log(`\n${bio}`); console.log("\nYou Can Connect with me here:"); console.log(chalk.cyan("GitHub:"), Github); console.log(chalk.cyan("LinkedIn:"), Linkedin); console.log(chalk.cyan("Portfolio:"), Portfolio); // Here, I've added my socials links and used chalk to provide font colors. // You can also set background colors using chalk.
Now, let's add some animation using chalk-animation for the closing words
let rainbow = chalkAnimation.rainbow( ` Thank you for checking out my CLI application!` ); // This part creates an animation effect for the closing words using chalk-animation. await new Promise((resolve) => setTimeout(resolve, 5000)); // Adjust the time delay as needed. rainbow.stop();
let rainbow = chalkAnimation.rainbow( ` Thank you for checking out my CLI application!` ); // This part creates an animation effect for the closing words using chalk-animation. await new Promise((resolve) => setTimeout(resolve, 5000)); // Adjust the time delay as needed. rainbow.stop();
Run
node index.jsto see the changes in the UI.Finally, to publish your package to npm, make changes in
package.json
"bin": { "app-name": "index.js" } // Create an npm account and run npm login // then after login just run npm publish npm publish
"bin": { "app-name": "index.js" } // Create an npm account and run npm login // then after login just run npm publish npm publish
Once your package is published, you can run the command using the name specified in
package.json. For instance, in my case, it'sakshaygore, so I would run:
npx akshaygore
npx akshaygore
Summary
And there you go! You have your CLI bio ready to share with your friends. If you encounter any issues during the creation process, feel free to run the above command and connect with me on social media. I'm happy to assist you!
Your feedback on my blog is much appreciated. Let me know how I can improve it further!
Introduction
Would you like to create a CLI (Command Line Interface) application with your friends? Follow these steps to get started:
Create a new directory for your CLI app
mkdir cli-app cd cli-app npm init
Install the necessary packages for beautiful text and colors
npm install chalk chalk-animation figlet
Create a new file named
index.jstouch index.jsAdd the following line at the top of the
index.jsfile
The line#!/usr/bin/env nodeat the beginning of a JavaScript file is called a shebang line. It instructs the system to
use the Node.js interpreter to run the script.
#!/usr/bin/env node
Create an Immediately Invoked Function Expression (IIFE) to begin the script. Let's start by using Figlet
(async () => { await figlet.text( "Hey There", { font: "Standard", horizontalLayout: "default", verticalLayout: "default", width: 70, whitespaceBreak: true, }, function (err, data) { if (err) { console.log("Something went wrong..."); console.dir(err); return; } console.log(data); } ); })(); // Figlet offers extensive documentation for further customization.
Now, let's utilize chalk with simple
console.log()statements and template literals to customize the console logs
let Github = "https://github.com/Akshaygore1"; let Linkedin = "https://www.linkedin.com/in/akshaygore2301/"; let Portfolio = "https://akshaygore.tech"; let bio = `My name is ${chalk.bold.cyan( "Akshay Gore" )}. I am a Full Stack Developer proficient in ${chalk.yellow( "JavaScript" )} and ${chalk.blue("TypeScript")}.`; console.log(`\n${bio}`); console.log("\nYou Can Connect with me here:"); console.log(chalk.cyan("GitHub:"), Github); console.log(chalk.cyan("LinkedIn:"), Linkedin); console.log(chalk.cyan("Portfolio:"), Portfolio); // Here, I've added my socials links and used chalk to provide font colors. // You can also set background colors using chalk.
Now, let's add some animation using chalk-animation for the closing words
let rainbow = chalkAnimation.rainbow( ` Thank you for checking out my CLI application!` ); // This part creates an animation effect for the closing words using chalk-animation. await new Promise((resolve) => setTimeout(resolve, 5000)); // Adjust the time delay as needed. rainbow.stop();
Run
node index.jsto see the changes in the UI.Finally, to publish your package to npm, make changes in
package.json
"bin": { "app-name": "index.js" } // Create an npm account and run npm login // then after login just run npm publish npm publish
Once your package is published, you can run the command using the name specified in
package.json. For instance, in my case, it'sakshaygore, so I would run:
npx akshaygore
Summary
And there you go! You have your CLI bio ready to share with your friends. If you encounter any issues during the creation process, feel free to run the above command and connect with me on social media. I'm happy to assist you!
Your feedback on my blog is much appreciated. Let me know how I can improve it further!
Dive Deeper with AI
On this page
Get exclusive blogs, articles and videos on data streaming, use cases and more delivered right in your inbox!
Ready to Switch to Condense and Simplify Real-Time Data Streaming? Get Started Now!
Switch to Condense for a fully managed, Kafka-native platform with built-in connectors, observability, and BYOC support. Simplify real-time streaming, cut costs, and deploy applications faster.



