19

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?

5
  • This could help u. Commented Nov 12, 2016 at 10:10
  • 1
    isnt that exactly what i do?] Commented Nov 12, 2016 at 10:24
  • is ~/bin/cl9wnhook works? Commented Nov 13, 2016 at 18:23
  • Nope :( same error. Seems like the command is registered but the file is not being found :( Commented Nov 15, 2016 at 8:51
  • 1
    Did u get any solution? I am also facing the same issue. Commented Jun 15, 2017 at 6:32

6 Answers 6

19
npm link

You would need to create a symlink for the mentioned folder. This would create a global symlink (without arguments).

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks! You probably don't need to use sudo.
This works, and I also didn't need to use the sudo.
5

Try adding

#!/usr/bin/env node

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

1 Comment

Yep, worked after adding to typescript file
3

your bin in your package.json like this:

{
"bin": {
    "app": "bin/app"
  },
}

but first in your app file you should add at the top this #!/usr/bin/env node

Note

This will help you

Comments

1

For a more complete answer in relation to using "bin" with npm link ;

  • The package.json of the linked library needs to include a "bin" property which points to the .js file you wish to use, most likely index.js. So "bin": { "my-package-name": "lib/index.js" }
  • The .js file needs to have #!/usr/bin/env node on the first line.
  • The .js file needs to have correct permissions to execute sudo chmod +x lib/index.js
  • To allow your package to be linked, you need to run npm link in the root directory of the package, which includes the package.json containing the name of the package, i.e. my-package-name
  • To link that package to another project, you need to run npm link my-package-name in the root directory of that project, which contains its own package.json
  • To check the link was successful, you need to run npm list, which should show your package linked in the current projects

Comments

0

Pack the package

npm pack

Then install it globally to run it from any folder.

npm install --global <package_file>.tgz

Comments

0

Really things can mess up from time to time when working with NPM or Yarn. In these cases first do a complete clean install.

rm -rf ./node_modules
yarn install
npm install

If that doesn't solve your problem then check the bin property in package.json, if it is mapping to the cli file correctly?

You can check what is packed by running yarn pack.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.