JavaScript is now the most popular programming language. Node.js is no longer limited to web development but also allows us to develop command line applications, CLI, or server side applications with JavaScript. This article will explain how to create a Node.js CLI project.
Table of Contents
Creating a Node.js Project
First, create a folder, execute npm init -y
under the folder, and a project is created! You can see a package.json file is generated under the folder. It is the configuration file of a Node.js project.
% mkdir hello-cli % cd hello-cli % npm init -y % ls package.json
Let’s take a quick look at package.json.
name
: Project nameversion
: Project versionmain
: The entry file of the project. The default file is index.js, but NPM will not automaitcally generate it.
{ "name": "hello-cli", "version": "1.0.0", "description": "", "main": "index.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, "author": "", "license": "ISC" }
Creating a Node.js CLI Script
Create an index.js file and fill in the code below. The first line tells the OS to execute this script file with node
command.
#!/usr/bin/env node const [,, ... args] = process.argv; console.log(`Hello ${args}`);
Let’s execute index.js. Set index.js as an executable file, and execute it.
hello-cli % chmod +x index.js hello-cli % ./index.js Wayne Hello Wayne
Setting Project’s CLI
The main
in package.json is the entry point when other projects import hello-cli. But hello-cli is a CLI project, not a Node.js module project, so we can remove it.
We add a property named bin
in package.josn, and add a property named hello
under the bin
. This hello
is a CLI command executing index.js. After users install hello-cli, they can run hello
under the command line.
{ "name": "hello-cli", "version": "1.0.0", "description": "", "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, "author": "", "license": "ISC", "bin": { "hello": "index.js" } }
Installing a CLI Project to the Local System
We just created hello
command, but without installing the project, we cannot test it. However, we also don’t want to test our project by directly executing index.js.
Under the project folder, we can install the project to the local system by executing npm link
.
hello-cli % npm link
Then you can execute the project locally.
% hello Wayne Hello Wayne
Use the following command to uninstall it from the local sytem.
hello-cli % npm unlink
Adjusting Directory Structure of CLI Projects
Now let us adjust the directory structure of the CLI project. Generally speaking, a CLI project looks like the following. The entry file will be under the bin folder. In the lib folder, we will put our own modules.
hello-cli % tree ├── bin │ └── cli.js ├── lib │ └── lib.js ├── package-lock.json └── package.json
In package.json, adjust the path of the hello
command.
{ "name": "hello-cli", "version": "1.0.0", "description": "", "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, "author": "", "license": "ISC", "bin": { "hello": "bin/cli.js" } }
Add a file named lib/lib.js, and past the following code:
exports.sayHello = function (to) { console.log(`Hello ${to}`); }
Move index.js to bin/cli.js, and change it’s content to the following code.
#!/usr/bin/env node const [,, ... args] = process.argv; const sayHello = require('../lib/lib').sayHello; sayHello(args);
Publishing to NPM
Finally, with the following command, you can publish the hello-cli project to NPM Repository. The following –access=public refers to publishing to the public Repository. After publishing it, anyone can download and install your project. If you want to publish to a private repository, you have to pay.
% npm publish --access=public
Conclusion
After creating a Node.js project, we can feel that Node.js provides us with a fast development approach. And this approach allows you to develop Web applications (such as React/Angular/Vue.js), CLI applications, and Server side applications. This diversity is that other development environments cannot provide.