Node.js/NPM CLI Information
Creating a CLI with Termivore is pretty easy, especially if you use the Termivore CLI to do so. However, if you have opted not to setup your CLi with the Termivore CLI, or you want to know more about how it works then please read this page.
Your package.json
File
There are a few things you will want (and need) to do with your project's package.json
file before you can start using your CLI.
Basic requirements for your package.json
Name
You will be required to have a name which you most likely already will have. One thing to keep in mind with the name field in your package.json
is that if you intend to publish your CLI as a package you will need to ensure the name isn't already in use by another package. If you're not publishing your package publically, then this isn't as important.
Scoped Packages
You may have noticed some packages are scoped
meaning the package name is prefixed with @organisation-name/
. This is totally possible with Termivore, just ensure the name in your package.json
includes the scope. For example if you have an organisation on NPM called 'my-org' you wish to publish your package to, you'll need to set the package.json
name to @my-org/package-name
.
See more about NPM scopes here
Version
The version field is pretty straight forward, you just need to have a valid version string assigned to it. It's recommended you follow semantic versioning(semver) rules though. This means you have a MAJOR.MINOR.PATCH
formatted version.
Read more about Semantic Versioning
Bin
The bin field in your package.json
is where you define what your CLI's root command will be and the file that will be executed when someone runs said command. You can have multiple root commands if you wish to.
To ensure your command will run properly, you can define your bin field like so:
If you had multiple root commands you would just add them to the bin object. If you want to make it cleaner and your root command is the same as your package.json name you can define it like this:
Which would be the same as doing:
Read more about the package.json bin field
Your CLI's 'Entry' File
There's one more thing to do to ensure your CLI works as intended once you've got your package.json
file set up correctly, and that happens in the entry file you defined in the bin
field.
You will need to make your file exectuable which you do by including this line at the top of your file (before anything else in the file)
If this line isn't at the top of the file you declared as your entry file in your package.json
bin field then it won't work when someone tries to use your CLI.
Using your CLI locally
We have a whole page on using your CLI locally which you can see here.
Publishing your CLI
We have a whole page on publishing your CLI which you can see here.