How do I install a module globally using npm?
If you want to install a npm module globally, make sure to use the new -g
flag, for example:
npm install forever -g
The general recommendations concerning npm module installation since 1.0rc (taken from blog.nodejs.org):
- If you’re installing something that you want to use in your program, using require('whatever'), then install it locally, at the root of your project.
- If you’re installing something that you want to use in your shell, on the command line or something, install it globally, so that its binaries end up in your PATH environment variable.
I just recently used this recommendations and it went down pretty smoothly. I installed forever globally (since it is a command line tool) and all my application modules locally.
However, if you want to use some modules globally (i.e. express or mongodb), take this advice (also taken from blog.nodejs.org):
Of course, there are some cases where you want to do both. Coffee-script and Express both are good examples of apps that have a command line interface, as well as a library. In those cases, you can do one of the following:
- Install it in both places. Seriously, are you that short on disk space? It’s fine, really. They’re tiny JavaScript programs.
- Install it globally, and then npm link coffee-script or npm link express (if you’re on a platform that supports symbolic links.) Then you only need to update the global copy to update all the symlinks as well.
The first option is the best in my opinion. Simple, clear, explicit. The second is really handy if you are going to re-use the same library in a bunch of different projects. (More on npm link in a future installment.)
I did not test one of those variations, but they seem to be pretty straightforward.
On a Mac, I found the output contained the information I was looking for:
$> npm install -g karma
...
...
> [email protected] install /usr/local/share/npm/lib/node_modules/karma/node_modules/socket.io/node_modules/socket.io-client/node_modules/ws
> (node-gyp rebuild 2> builderror.log) || (exit 0)
...
$> ls /usr/local/share/npm/bin
karma nf
After adding /usr/local/share/npm/bin
to the export PATH
line in my .bash_profile
, saving it, and source
ing it, I was able to run
$> karma --help
normally.