Npm executables
August 17, 2016
A great feature of Npm and node is the easiness to create executable standalone programs.
I personally try to avoid installing executables globally (it happen every time you run npm i xxx -g
). Instead, I favour having explicit dependencies end purely rely on those. In such cases, to run executables from locally installed modules, we need to get the path to the node_modules/.bin, the folder where Npm install executables.
I.e: to run mocha locally, we would do something like:
Npm bin
Npm come with a bin command that simply return the folder where executables are installed.
We don’t have to worry for the relative path now, as we can run this command anywhere within the project dir.
Npm scripts
If you find your self doing a similar thing within your Npm scripts, i.e.:
Just be aware that Npm scripts will resolve to locally installed executables out of the box, so the above could simply be written as:
Conclusion
Try avoiding installing global packages as much as possible If you find yourself having hard times typing $(npm bin)/whataver you can just install one global package: npm-run (a nicely written wrapper around npm bin)
feel free to add an alias to your bashprofile, I personally like to have ‘nr’ so that I can simply type:
Wondering what packages you have already installed globally? Simply type: