How do I install a module globally using npm?
node.jsNpmnode.js Problem Overview
I recently installed Node.js and npm module on OSX and have a problem with the settings I think:
npm install [MODULE] is not installing the node.js module to the default path
which is /usr/local/lib/node_modules.
node.js Solutions
Solution 1 - node.js
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.
Solution 2 - node.js
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.
Solution 3 - node.js
I like using a package.json file in the root of your app folder.
Here is one I use
nvm use v0.6.4
npm install
Solution 4 - node.js
I had issues installing Express on Ubuntu:
If for some reason NPM command is missing, test npm command with npm help
. If not there, follow these steps - http://arnolog.net/post/8424207595/installing-node-js-npm-express-mongoose-on-ubuntu
If just the Express command is not working, try:
sudo npm install -g express
This made everything work as I'm used to with Windows7 and OSX.
Hope this helps!
Solution 5 - node.js
You need to have superuser privileges,
sudo npm install -g <package name>
Solution 6 - node.js
Recommended steps as per official doc worked for me on my Macbook.
Summary steps:
-
Instance nvm using following command:
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.38.0/install.sh | bash
You can check the latest version on this page: https://github.com/nvm-sh/nvm
-
create .zshrc at home directory if file is already not present.
touch .zshrc
-
put following content in .zshrc file
> export NVM_DIR="$HOME/.nvm" [ -s "$NVM_DIR/nvm.sh" ] && . > "$NVM_DIR/nvm.sh" # This loads nvm
- Install nvm using command
> nvm install --lts
- restart terminal - you are ready to install global package using npm now.
Solution 7 - node.js
In Ubuntu, set path of node_modules in .bashrc file
> export PATH="/home/username/node_modules/.bin:$PATH"
Solution 8 - node.js
You might not have write permissions to install a node module in the global location such as /usr/local/lib/node_modules
, in which case run npm install -g package as root.