Create Node Command line App
I wanted to create a command line Node Module. To start with I created my project directory change_oc and initialize my project with `npm init` excepting the defaults
I created the index.js file and gave it the following shebang
I edited the package.json file and add bin , and gave it my package name and pointed it at my main node file index.js
I needed to change permissions on the index.js file so it can run in the os
chmod 775 index.js
So that’s it the package is now able to execute from the command line but it doesn’t do anything so add a console.log to index.js to test.
Install the package globally from the package directory, and you can then run it by using the value set with bin in package.json e.g.
$ npm install -g
$ change_oc
This is like Echo in the command prompt
At this point you think great but what can I do. Node has some inbuilt components that are very useful for command line applications.
A simple example and use case
Note: I recommend using synchronous commands, asynchronous commands may not always execute in the order you wish if you are waiting for user input or a disk writing processes.
That’s the basics you may want to publish to npmjs.
Note: To use command line node modules in the os they need to be installed globally.
My cli project is on npmjs and github for further reference.