user3601578 user3601578 - 3 months ago 13
Linux Question

NodeJS app as CLI tool

I have Ubuntu Linux and a CLI NodeJS app which I start with:

$ node myapp.js param1 value1 param2 value2


Now I want to start myapp.js like this:

$ myapp param1 value1 param2 value2

Answer

First, add a shebang to the beginning of the file:

#!/usr/bin/env node

Copy to somewhere in your PATH (using /usr/local/bin as an example):

$ cp myapp.js /usr/local/bin/myapp

Finally, make it executable:

$ chmod +x /usr/local/bin/myapp

Now your program can be run as:

$ myapp param1 value1 param2 value2

(If the program has dependencies, you will need to install them globally, with the -g flag)