Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node package.json "bin" value, command not working

I used nodejs on my vserver to make a tiny script to manage users in a db.

In package.json I added the "bin" and set it to my script. My attempt was to make a command available on the whole server so I dont need to go to the directory where the script lies and write "node usermanager.js".

I used npm link and it seemed to work fine:

/home/sl4yer/bin/cl9wnhook -> /home/sl4yer/lib/node_modules/cl9wnhook_usermanager/usermanager.js
/home/sl4yer/lib/node_modules/cl9wnhook_usermanager -> /home/sl4yer/cl9wnHook/usermanager

package.json btw is:

{
  "name": "cl9wnhook_usermanager",
  "version": "1.0.0",
  "description": "User manager for cl9wnHook",
  "main": "usermanager.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "bin": {
    "cl9wnhook": "./usermanager.js"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "commander": "^2.9.0",
    "js-sha512": "^0.2.2",
    "readline-sync": "^1.4.5"
  }
}

so using the command "cl9wnhook" should work.

But when I call it, I get:

[sl4yer@lynx usermanager]$ cl9wnhook
: No such file or directory

Any idea?

like image 450
SVARTBERG Avatar asked Nov 12 '16 09:11

SVARTBERG


3 Answers

sudo npm link

I did it, and successfully.

like image 95
Haha Wa Avatar answered Oct 03 '22 02:10

Haha Wa


Try adding

#!/usr/bin/env node

on the top of your usermanager.js file. It should work.

like image 30
Pankaj Tanwar Avatar answered Oct 03 '22 02:10

Pankaj Tanwar


Pack the package

npm pack

Then install it globally to run it from any folder.

npm install --global <package_file>.tgz
like image 37
Rehmat Avatar answered Oct 03 '22 04:10

Rehmat