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:

$ node_modules/.bin mocha

Npm bin

Npm come with a bin command that simply return the folder where executables are installed.

$ $(npm bin)/mocha

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.:

"scripts": {
  "test": "node_modules/.bin mocha"
}

Just be aware that Npm scripts will resolve to locally installed executables out of the box, so the above could simply be written as:

"scripts": {
  "test": "mocha"
}

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)

$ npm i -g npm-run

feel free to add an alias to your bashprofile, I personally like to have ‘nr’ so that I can simply type:

$ nr jspm init // will run the locally resolved jspm bin executable like $(npm bin)/jspm init

Wondering what packages you have already installed globally? Simply type:

$ npm list -g depth=0 // leave out the depth flag to get scared
comments powered by Disqus