How to make bin script available for npm package installed locally
Asked Answered
F

2

7

I read on the npm documentation that you can't use bin scripts of locally installed packages.

So, how gulp can be launched as bin command when installed locally? What's making it available when locally installed, I reviewed the gulp package.json and the bin scripts, I don't found any answer.

Fidelia answered 26/8, 2016 at 9:7 Comment(1)
Please mark the question as answered if your problem is solved. Otherwise, let us know what is not working yet.Seleucia
S
6

From NPMJS documentation:

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.

So, your locally installed packages binaries will be executable like this

./bin/node_modules/.bin/the_binary

This is if you want to launch the binary directly. Or, as specified in the scripts part of the documentation:

In addition to the shell's pre-existing PATH, npm run adds node_modules/.bin to the PATH provided to scripts.

Thus, you can simply write a wrapper script like

scripts: {
  "build": "the_binary"
}

and call your script like this

npm run build

Bonus

As of [email protected], you can use custom arguments when executing scripts. The special option -- is used by getopt to delimit the end of the options. npm will pass all the arguments after the -- directly to your script:

npm run test -- --grep="pattern"
Seleucia answered 26/8, 2016 at 9:22 Comment(3)
So, I cannot install a package (gulp as example) locally and run the bin command without aving a global install? Cause I have installed Gulp locally and I can run gulp default bin command without having Gulp installed globally. The problem is maybe in my bash_profile... I dont know why I can run Gulp but not my custom bin command without global install and the documentation you linked seems to say it is totaly normal.Fidelia
No, you should'nt be able to do that. You can run npm list -g to see which packages are installed globally and which gulp to see where the binary is located.Seleucia
I confirm, the problem was from my bash_profile... Thanks a lot for your time!Fidelia
C
0

You can use lpx https://www.npmjs.com/package/lpx to

  • run a binary found in the local node_modules/.bin folder
  • run a binary found in the node_modules/.bin of a workspace root from anywhere in the workspace

lpx does not download any package if the binary is not found locally (ie not like npx)

Example : lpx tsc -b -w will run tsc -b -w with the local typescript package

Cowitch answered 16/5, 2021 at 11:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.