Cristian
Cristian

Reputation: 2501

How do I install a module globally using npm?

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.

Upvotes: 237

Views: 341959

Answers (9)

Andrii Los
Andrii Los

Reputation: 556

Since npm version 8 -g or --global flag is deprecated as per warning I'm getting in the console (official docs yet to be updated):

npm WARN config global `--global`, `--local` are deprecated. Use `--location=global` instead.

So new command to install modules globally is

npm install forever --location=global

Where forever is the name of the package you want to install.

Upvotes: 0

Rajeev Jayaswal
Rajeev Jayaswal

Reputation: 1501

Recommended steps as per official doc worked for me on my Macbook.

Summary steps:

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

  1. create .zshrc at home directory if file is already not present.

    touch .zshrc

  2. put following content in .zshrc file

export NVM_DIR="$HOME/.nvm" [ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh" # This loads nvm

  1. Install nvm using command

nvm install --lts

  1. restart terminal - you are ready to install global package using npm now.

Upvotes: 0

fullstacklife
fullstacklife

Reputation: 2061

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

Upvotes: 5

Saurav Kumar
Saurav Kumar

Reputation: 970

You need to have superuser privileges,

 sudo npm install -g <package name>

Upvotes: 3

Ratnesh Kushwaha
Ratnesh Kushwaha

Reputation: 368

In Ubuntu, set path of node_modules in .bashrc file

export PATH="/home/username/node_modules/.bin:$PATH"

Upvotes: -1

Dmitri Bouianov
Dmitri Bouianov

Reputation: 538

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.

Upvotes: -5

yurisich
yurisich

Reputation: 7109

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 sourceing it, I was able to run

$> karma --help

normally.

Upvotes: 8

Cody
Cody

Reputation: 10015

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!

Upvotes: 2

schaermu
schaermu

Reputation: 13460

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.

Upvotes: 398

Related Questions