skip to content

building a CLI with NodeJS

Last updated: Aug 7, 2024

I recently created a little CLI for my own use to run an ffmpeg command to transform a series of images into a video (I have some notes on that process here: ffmpeg commands for image sequences). Here are a few notes on the process of building a CLI with NodeJS!

very basic setup:

  1. Create an empty directory, cd into it, and npm init or pnpm init.

  2. Create index.js in your new directory. At the top, add a shebang like this: #! /usr/bin/env node. This tells our shell which interpreter to use (in this case, NodeJS).

  3. Add a bin entry to your package.json.

    "bin": {
    "mycli": "index.js"
    },
    "type": "module"

    This sets your cli command as mycli and index.js as the file which will be run when you use that command. I also added type to use ES module syntax but that part is optional.

  4. In the same directory, run this command to install your command globally:

    npm install -g .
  5. Add shebang at the top of index.js - this tells our shell which interpreter to use (in this case, NodeJS):

    #! /usr/bin/env node

Resources