On global installation of node modules
In npm 1.0, there are two ways to install things:
globally —- This drops modules in
{prefix}/lib/node_modules
, and puts executable files in{prefix}/bin
, where{prefix}
is usually something like/usr/local
. It also installs man pages in{prefix}/share/man
, if they’re supplied. locally —- This installs your package in the current working directory. Node modules go in./node_modules
, executables go in./node_modules/.bin/
, and man pages aren’t installed at all.
Why ever one would want to install a npm module system wide? The answer is: if it used in many projects, or if it's CLI. Yes, even if this CLI module is used in single project, npm
developers recommend us to install it globally. And this globally is not even user-wide, that's why it requires root rights.
But.. hey, are we developers from 80s declaring global variables here and there?
Pros and cons of global installation
Global installation is simple and dirty that's why i already love it. But global installation requires root privileges and pollutes global name space of CLI commands of the whole system. So its unsecure and may introduce hard to find errors in scripts. Moreover, you loose ability to use different versions of CLI commands in different projects, or — from another point of view — to link the project to fixed versions of modules it uses.
How to run CLI module installed locally
npm
just places local modules in hidden directory .bin
inside node_modules
, so we can execute any module specifying full path of it:
node_modules/.bin/blabla
To make this simpler, we can add node_modules/.bin
to (user) PATH, for example, using .bashrc
:
export PATH="$PATH:./node_modules/.bin"
This allows to run local CLI modules, while you are in project's directory. It's not perfect, but for some cases it may be ok.
Cheers.
shitpoet@gmail.com