DEV Community

Bojan Jagetic
Bojan Jagetic

Posted on • Updated on • Originally published at bojanjagetic.com

Creating a Node.js Command-line Tool, Linux Terminal CLI and NPM Package

Introduction

Command-line tools are incredibly useful for automating repetitive tasks, performing quick checks and for developers to perform different tasks. In this tutorial, we will go through the process of creating a Node.js command-line tool, converting it into a Linux terminal CLI tool, and packaging it as an npm package.

Creating a Node.js CLI

Node.js provides a built-in "fs" (file system) and "readline" modules to read and write files, as well as the "process" object to access command-line arguments. Here is an example of a simple command-line tool that takes two arguments, a file path and a string, and writes the string to the file:

const fs = require("fs");
const readline = require("readline");

const filePath = process.argv[2];
const string = process.argv[3];

fs.writeFile(filePath, string, (err) => {
  if (err) {
    console.error(err);
    return;
  }
  console.log(`Successfully wrote to ${filePath}`);
});
Enter fullscreen mode Exit fullscreen mode

You can run this script using the command node script.js path/to/file "Hello, World!".

You can also use npm package commander to make more complex command line tool with lot of options and sub commands.

const program = require('commander');
program
  .version('0.0.1')
  .description('A simple command line tool')
  .option('-o, --output <file>', 'output file')
  .parse(process.argv);

console.log(program.output);
Enter fullscreen mode Exit fullscreen mode

You can run the above script using the command node script.js -o path/to/file.

Converting to a Linux Terminal CLI Tool

To make a Linux terminal command-line tool, you can use the same methods as mentioned earlier to create a Node.js script that accepts command-line arguments and performs the desired functionality. However, to make the script executable in the terminal as a command, you need to add a shebang line at the top of your script file and make the file executable.

The shebang line is the first line of the script and should be in the following format:

#!/usr/bin/env node
Enter fullscreen mode Exit fullscreen mode

This tells the operating system that the script should be executed using the Node.js interpreter.

Then you need to make your script file executable by running the following command in the terminal:

chmod +x script.js
Enter fullscreen mode Exit fullscreen mode

After that, you can move the script file to a directory that is in your system's PATH and you can run the script as a command from anywhere in the terminal.

mv script.js /usr/local/bin/your_command
Enter fullscreen mode Exit fullscreen mode

Now you can run your command from anywhere in the terminal using your_command

Packaging as an npm package

Once you have a functioning Node.js command-line tool, you can package it as an npm package. To do this, you first need to have a Node.js project that you want to package. The project should have a package.json file, which contains information about the package such as its name, version, and dependencies.

You can create a package.json file by running the following command in your project's root directory:

npm init
Enter fullscreen mode Exit fullscreen mode

This command will prompt you for information about your package, such as its name and version, and will create a package.json file in your project's root directory.

Once you have a package.json file, you can add your code files and other necessary files to your package. Make sure that the main file of your package is specified in the main field of package.json.

You can then use the npm pack command to create a tarball of your package, which can be published to the npm registry or distributed manually.

npm pack
Enter fullscreen mode Exit fullscreen mode

To publish your package to the npm registry, you will need to have an account on npm and be logged in. You can do this by running the following command:

npm login
Enter fullscreen mode Exit fullscreen mode

Once you are logged in, you can use the

npm publish command to publish your package to the npm registry.

npm publish
Enter fullscreen mode Exit fullscreen mode

It's important to note that once you have published a package with a specific version, you cannot publish another package with the same name and version. If you need to make changes to your package, you will need to increment the version number in your package.json file and republish the package.

If you want to check npm publishing step-by-step you can check my other post 5 Simple Steps to Creating Your Very Own npm Module.

Creating a Node.js Command-line Tool, Linux Terminal CLI and NPM Package

Command-line tools are incredibly useful for automating repetitive tasks, performing quick checks and for developers to perform different tasks.In this tutorial, we will go through the process of creating a Node.js command-line tool.

bojanjagetic.com

or earlier post on dev.to

Conclusion

In conclusion, creating a Node.js command-line tool is a great way to automate repetitive tasks and perform quick checks. By converting it into a Linux terminal CLI tool and packaging it as an npm package, you can easily share your tool with others and make it easy for them to use. With the help of fs and readline modules, you can easily read and write files and access command-line arguments. Additionally, you can use the commander npm package to make more complex command line tools with a lot of options and sub-commands. Once you have a functioning Node.js command-line tool, you can package it as an npm package and share it on npm registry. This allows other developers to easily install and use your tool in their own projects.

Top comments (0)