[Node] How to create a CLI(Command-line interface) program in Node

Photo by Sai Kiran Anagani on Unsplash

While creating programs, we often feel the need to use the CLI or create a CLI.

I would like to explain how to create the CLI with the javascript language used by many pragrammers.

Creating a Node package

Use the nodes to create the CLI. To do this, we create a Node package.

Create a package using the command below. I am using Linux as my primary operating system, so I will write down commands based on it.

mkdir cli-programs && cd cli-programs && npm init

Enter the above command to display the settings for the package.

Our purpose is to practice, so we all press Enter to pass through the settings.

Node Project configuration

Creating a JavaScript executable

When package creation is complete, now create a JavaScript file to run on the CLI.

I will create a file named main.js in the package root directory.

Insert the following code at the top of the main.js file to inform the operating system that it is a node file executable on a Linux or UNIX computer.

#! /usr/bin/env node

The above code is ignored in the window.

Note) The #! mark is called Shebang Character Sequence.

Run JavaScript File

First of all, Linux requires permission to run files. Use the command below to grant executable permissions to JavaScript files. Write sudo before the command because it requires administrator privileges.

sudo chmod +x main.js

Note) https://en.wikipedia.org/wiki/Chmod

Let’s write down the code.

Line 4 is a grammar called Destructuring Assessment.
Here, you receive the value you entered when you ran the JavaScript file.

If you look at the results of the implementation below, you will understand what I mean.

In the project folder, run the JavaScript file with the following command:

./main.js

When you run a file, you can also give the input value as shown below.

Using Commands in a Node Package

Command Name Definition

Now, in order to use the code we created as CLI in Node, we need to enter it in the package.json file as shown in the picture below.

“Hello-world” is the name of the command, and we can use this keyword to execute the above-made main.js.

You can find out more about this setting on the official website.

Package linking

Now, setting up is necessary to use the commands we have created globally.

Enter the following command into the project folder

sudo npm link

Linux also requires administrator privileges, so it enters sudo before the command.

The npm link creates a symbolic link in the global node_modelus.

This is handy for installing your own stuff, so that you can work on it and test it iteratively without having to continually rebuild.

Now we can run commands in any directory.

You can remove a symbolic link from the project directory by using the following command:

sudo npm unlink -g

Node) When you deploy the package to npm, it is installed globally when you install it through the npm install -g <package name> command. Therefore, you do not need to do the above.

Conclusion

We’ve looked at how to create a CLI program in Node.

Node allows you to quickly and conveniently create CLI programs that work on various platforms.

If I have a chance, I will also post an introduction to the CLI program that I made.

I hope this article helped you and I hope you enjoy coding. 😄

https://www.buymeacoffee.com/jayflow

I love Programming 🧑🏻‍💻 https://github.com/Jay-flow