DEV Community

Cover image for Building Your First CLI Tool With Nodejs
Nurofsun
Nurofsun

Posted on

Building Your First CLI Tool With Nodejs

I thought that JavaScript just works in browser, I've wrong opinion of this amazing programming language, it's actually can run on your command line paired with popular platform called nodejs.

In this article, I'll show you how to make simple CLI tool with javascript, let's get started.

Initialize The Project

First of all, we’ll create new folder for example hello-cli and run the initialization with npm or yarn.

$ npm init
# or maybe you wanna use yarn
$ yarn init
Enter fullscreen mode Exit fullscreen mode

Inside that directory create file called index.js and fill this with a simple code shown below.

console.log('Hello World');
Enter fullscreen mode Exit fullscreen mode

And run it, with this command.

$ node .
# output
Hello World
Enter fullscreen mode Exit fullscreen mode

As you can see above we receive an output Hello World.

How ashamed is it? Says Patrick

Well, okay if you can create it why don’t you tell me about the guide to making such thing, so I would never go to search engine and looking for it on the internet.

Arrgghh, Let’s forget about patrick! and continue the project, everything will be fine.

Make The File as Regular Shell Command

To achieve this thing, we must to add #!/usr/bin/env node at the top of our index.js.

And make it possible to executable even without node command.

#!/usr/bin/env node
console.log('Hello World');
Enter fullscreen mode Exit fullscreen mode

Update our package.json

Next we need to updating our package.json by adding new property called bin.

bin property basically specify command name for node project, but in this case we only have a single file in our project, so the name property will be used as shell command name.

{
  "name": "hello-cli",
  "bin": "./index.js",
  "version": "1.0.0",
  "main": "index.js",
  "license": "MIT"
}
Enter fullscreen mode Exit fullscreen mode

Make Package Globally Available

For now, our app not globally available yet, that means we cannot run it with hello-cli command.

But, in this case also we don’t want to run our application as hello-cli.

Instead, we want to run this application by typing only hello, so to do this we also update the name property.

Updated package.json as shown below.

{
  "name": "hello",
  "bin": "./index.js",
  "version": "1.0.0",
  "main": "index.js",
  "license": "MIT"
}
Enter fullscreen mode Exit fullscreen mode

Now we change the index.js file permission by doing chmod +x index.js otherwise , you will received permission denied message when trying to run it.

Then, after that we run command npm link or yarn link on the project directory. Since you run the command above you make symbolic link, and now hello package available as global package.

Let’s start to run our application, by running hello command in our shell/cmd.

$ hello
# output
Hello World
Enter fullscreen mode Exit fullscreen mode

Wow ! it is work! Thanks for reading.

Original Post

Top comments (0)