user3601578
user3601578

Reputation: 1362

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

Upvotes: 1

Views: 147

Answers (2)

nloveladyallen
nloveladyallen

Reputation: 184

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)

Upvotes: 2

owais
owais

Reputation: 4922

use nexe to convert your node script into binary which you can put in usr/local or any other folder which is added into your env variables so that you can run from commandline.

npm install nexe -g  

nexe -i ./myapp.js -o ./myapp  

Upvotes: 2

Related Questions