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.
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.
sudo chmod +x main.js
Let’s write down the code.
Line 4 is a grammar called Destructuring Assessment.
If you look at the results of the implementation below, you will understand what I mean.
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.
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.
There are already many good packages available to help with the CLI program in Node.
The complete solution for node.js command-line interfaces. Read this in other languages: English | 简体中文 For information…
A collection of common interactive command line user interfaces. Inquirer.js strives to be an easily embeddable and…
Terminal string styling done right Chalk comes with an easy to use composable API where you just chain and nest the…
Elegant terminal spinner $ npm install ora If a string is provided, it is treated as a shortcut for options.text …
created the CLI program using the package above and it was very useful. There are many other good packages, so please look for them at Npm.
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. 😄