Publish a barebones CLI

ricsam

Richie

Posted on November 9, 2022

Publish a barebones CLI

You don't have to setup a bunch of tooling to just get started with a simple CLI.

A package with little code is often a better start as an open source package because it is easier for contributors to help initially in a lightweight repository.

To create a CLI make a folder:

mkdir my-cli
Enter fullscreen mode Exit fullscreen mode

In this folder run:

npm init -y
cat <<EOF > cli.js
#!/usr/bin/env node
console.log('my cli');
EOF
chmod +x cli.js
Enter fullscreen mode Exit fullscreen mode

open the created package.json file and add:

"bin": {
  "my-cli": "cli.js"
},
Enter fullscreen mode Exit fullscreen mode

To test the cli in your shell run npm link while in the package folder.

You should now be able to run my-cli in any terminal on your local machine.

To publish to npm update the package.json file with the correct details and run npm publish.

Other people can now run npm install -g my-cli to use your CLI.

💖 💪 🙅 🚩
ricsam
Richie

Posted on November 9, 2022

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

Publish a barebones CLI
javascript Publish a barebones CLI

November 9, 2022