The Intermediate Guide to Create A Command-line App In Node.js





Many developers use command-line apps on each day foundation.

From git to yarn, we use many CLI (Command Line Interface) apps. no longer each person likes CLI over GUI, but many geeks love the usage of CLIs.

these days, we can discover ways to create Command-Line Apps in Node.js

Command-Line apps can be written in lots of languages. in case you write them in Node.js, you could serve your app to the NPM/Yarn customers.

Assumptions
  • you understand how to deploy NPM applications.
  • you recognize the fundamentals of Node.js
  • you recognize primary terminal commands like cd, PWD, ll, ls.

Get started out
cd to the repository in which you store all of your code.

Do the subsequent...
$ mkdir demo-cli && cd demo-cli
$ touch index.js
$ npm init -y

Open the demo-cli folder in your favorite code editor.
index.js
CLI apps are all approximately enter and output. in this Starter manual, we might not pass deep into developing a CLI that does something critical.

For now, we can most effectively make use of chalk and figlet to do make some creative output. Get more info to follow us Nodejs Online Training India 

Run the subsequent command to put in wanted applications

$ npm i chalk figlet clear -s

And then, just paste the following snippet in your index.js.
#!/usr/bin/env node

const clear = require('clear')
const chalk = require('chalk')
const figlet = require('figlet')

clear()

console.log(
  chalk.yellow.bold(
    figlet.textSync('CLI!', {
      horizontalLayout: 'full'
    })
  )
)

The #!/usr/bin/env node line needs to be at the top for a Command-Line App to work.

package.json

Add a Start Script in your package.json...
{
  "scripts": {
    "start": "node index.js"
  }
}

Also, add a bin script. That script decides what command will the user needs to type to see the output.
{
  "bin": {
    "thisDemoCli": "index.js"
  }
}

See the outputRun npm start to see how the output looks like. Isn't it just like running a Node app?Okay, now, do this
$ pwd # Copy the output you get

$ npm i -g <PASTE_THE_OUTPUT_OF_PREV_COMMAND>
And now try running thisDemoCli in the terminal, and you should see the same output!

Publish to NPM

To publish to NPM so that you can serve your apps to users, give your package a unique name, and provide it a version.
And then run
$ npm publish
 Get in-depth knowledge about nodejs with angular to read nodejs online training Hyderabad from onlineitguru.com with the help of realtime project 

Comments

Popular posts from this blog

Informatica Knowledge Base (IKB)

Installing the On-Premises Data Gateway in Power BI

Is Power BI available for Mac?