First CLI App using Node JS for Beginner’s

Ranjit mohite
3 min readDec 3, 2020

--

Prerequisite:

Step-1:

  • Create new repl
  • Inside it, Choose JS or NodeJS to create a javascript repl.

Step-2:

Installing package for use code written by others via this system.

1)readlineSync

NPM, or Node Package Manager, helps us utilize 3rd party packages.

The readline module provides an interface for reading data from a Readable stream one line at a time. It can be accessed using:

Simple Case:

var readlineSync = require('readline-sync');var userName = readlineSync.question('May I have your name? ');console.log('Hi ' + userName + '!');

These packages will help us solve many problems by taking input from the user.

2)chalk:

The chalk library offers powerful and simple methods to enforce ANSI colors and styles to our terminal output.

//index.jsconst chalk = require('chalk');

console.log(chalk.red("Lost in cosmos!"));

Step-3:

Using “Array” to Store Multiply value at a time.

An array is a special variable, An array is used to store a collection of data, but it is often more useful to think of an array as a collection of variables of the same type.

Syntax:

var fruits = new Array( “delhi” ,“kashmir” ,“maharashtra” );

Step-4) Using Functions,Condition,for loop.

Function:

A JavaScript function is defined with the function keyword, followed by a name, followed by parentheses ().

Syntax:

function functionName(parameterOne, parameterTwo) {
// processing
return outputValue;
}

Condition:

Conditional statements are used to perform different actions based on different conditions.

  • Use “if” to specify a block of code to be executed, if a specified condition is true
  • Use “else” to specify a block of code to be executed, if the same condition is false
  • Use “else if” to specify a new condition to test, if the first condition is false
  • Use a “switch” to specify many alternative blocks of code to be executed

for loop:

Loops are handy, if you want to run the same code over and over again, each time with a different value.

Syntax:

for (statement 1; statement 2; statement 3) {
// code block to be executed
}

Console.log():

The console.log() is a function in JS that is used to print any kind of variables defined before in it or to just print any message that needs to be displayed to the user.

Step-5) Combine All the steps to create a CLI App.

Output:

Github link

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Ranjit mohite
Ranjit mohite

No responses yet

Write a response