Skip to content

Instantly share code, notes, and snippets.

@TasfiqulTapu
Last active September 4, 2022 15:58
Show Gist options
  • Save TasfiqulTapu/6046ae4cfb72f7a4a11a56f57ad2b44f to your computer and use it in GitHub Desktop.
Save TasfiqulTapu/6046ae4cfb72f7a4a11a56f57ad2b44f to your computer and use it in GitHub Desktop.
Create a nodejs CLI tool that is installable from NPM

Nodejs CLI tool

  1. Start off by first making a node project that can be operated from cli. You can get the cli arguments from process.argv which is an array containing all the command line arguments passed in.

  2. Put #! /usr/bin/env node on top of your index file(entry point for your project). This is called shabang and let's the OS know what to run the file with.

  3. Add the following to your package.json

    "bin": {
        "hello": "./index.js"
    },

    This will add tell npm to add the command named hello which will run the index.js file.

  4. Now running npm i -g will install your project as a CLI application.

  5. Test it.

  6. Test it.

  7. Test it.

  8. To just run npm publish after going through npm adduser. In order to keep the install size smol you can add "files": ["lib/**/*"], to your package.json so that npm publishes on the files isnide the lib folder. One other way would be to use .npmignore file which works just like .gitignore.

  9. Now anyone can install the cli app by running `npm i -g your_project_name".

this gist is brought to you by my procrastination and my goldfish brain

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment