Making a Node.js Command-Line Utility

How to make Node.js Command-line UtilitiesToday, we’re going to cover how to turn your Node.js script into a native command-line utility, and then how to share it with the world using NPM (Node Package Manager). In the video, I’m using the trial-and-error approach to show you everything that is required and show the errors you might encounter along the way if you forget a step. It’s actually quite simple.

Cheat Sheet

Turn your Node script into a native command-line utility

  1. Wrap your Node script in a package by putting it in its own directory and creating the package.json file (name and version are the only required properties)
  2. Expose your script as a command-line utility by adding the bin property to the package.json file, mapping the command-line utility’s name to your script
  3. In your script, add the shebang: #!/usr/bin/env node
  4. npm link to install the package globally

Share it with the world

  1. In the package.json file, add the preferGlobal property set to true.
  2. npm adduser
  3. npm publish

Unshare it with the world

  1. npm unpublish mypackage --force

Example package.json file (minimal)

    “name”: “mypackage”,
    “version”: “0.0.1”,
    “preferGlobal”: “true”,
    “bin”: {
        “mycommand”: “./relative/path/to/script.js”

About the Author

Author: Mike M. Lin

Mike M. Lin

In 2001, I helped build one of the first true highly interactive web apps at Siebel Systems, replacing slick, but slow, JavaScript UI controls with speedier ActiveX counterparts. I've been out of web development until recently. Web apps are a lot different now than in 2001, and I'm just trying to get my bearings in this new landscape.