Binary file in npm package

Actually, you can put your myapp.js file into bin.
So, the bin key in package.json file should be like this :

"bin": { "myapp" : "<relative_path_to_myapp.js>/lib/myapp.js" }

At the first line in myapp.js, you must add this shebang line :

#!/usr/bin/env node

It tells the system to use node to run myapp.js.


... Or if you don't want to call myapp.js directly, you can create a script like this to be your executable file :

#!/usr/bin/env node

var myapp = require('<relative_path_to_myapp.js>/myapp.js');
myapp.doSth();

and in package.json :

"bin" : { "myapp" : "<relative_path_to_the_script>/script.js" }

By doing this either way, you can avoid finding the path to your nodemodule.


But... if you insist to use your old myapp bash script, then you can find the path to the module with this :

myapp_path=$( npm explore -g myapp -- "pwd" )

Hope these help :D


https://docs.npmjs.com/files/package.json#bin

From the above link:

...

To use this, supply a bin field in your package.json which is a map of command name to local file name. On install, npm will symlink that file into prefix/bin for global installs, or ./node_modules/.bin/ for local installs.

For example, myapp could have this:

{ "bin" : { "myapp" : "./cli.js" } }

So, when you install myapp, it’ll create a symlink from the cli.js script to /usr/local/bin/myapp.

...