Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NPM modules won't install globally without sudo

People also ask

How do I get NPM packages installed globally?

To check for all globally installed packages and its dependencies, run the npm list command followed by the -g flag. This above command prints the all globally installed packages in tree view. You can also check if a specific package is installed globally or not using the npm list -g followed by package name.


Ubuntu 12.04 and using Chris Lea's PPA for install the following works for me:

npm config set prefix '~/.npm-packages'

and adding $HOME/.npm-packages/bin to $PATH

append to .bashrc

export PATH="$PATH:$HOME/.npm-packages/bin"

see https://stackoverflow.com/a/18277225 from @passy


If you already have $HOME/bin in your path, a simpler solution is just ...

npm config set prefix ~
  • New node commands will now install into your $HOME/bin directory.
  • No need to change your path!

Since this discussion is really about reducing the security risks of running sudo, you should also be aware that any node app could potentially be installing an app name that does not match the registered node package name you think you're installing. So there is a security risk that an npm install will replace an existing system command or one you already have in $HOME/bin. If you're concerned, check the bin, and scripts properties in the package.json file of the app you're installing first.

In general, it's safest to:

  • (a) Place $HOME/bin last in your path so system commands are not superseded.
  • (b) don't include "." or any relative path in your $PATH so you don't accidentally run a command that happens to be in the current directory.

Reference:

  • package.json properties
  • npm install
  • NodeJS security vulnerabilities: nodesecurity.io.

As for October 2014:

Node.js is available from the NodeSource Debian and Ubuntu binary distributions repository.

curl -sL https://deb.nodesource.com/setup | sudo bash -
sudo apt-get install -y nodejs

That's it.

Outdated answer:

The fastest way without using sudo is like described here by isaac

I strongly encourage you not to do package management with sudo! Packages can run arbitrary scripts, which makes sudoing a package manager command as safe as a chainsaw haircut. Sure, it's fast and definitely going to cut through any obstacles, but you might actually want that obstacle to stay there.

I recommend doing this once instead:

sudo chown -R $USER /usr/local

EDIT:

There are certain security concerns and functionality limitations regarding changing the ownership of /usr/local to the current user:

  • if there is another user on the machine who could use global npm packages - do not change the ownership of /usr/local
  • https://apple.stackexchange.com/questions/1393/are-my-permissions-for-usr-local-correct
  • https://askubuntu.com/questions/261326/is-it-safe-to-chown-usr-local

Having said that, if you want to install global module without using sudo, I don't see any better solution (from pragmatic point of view) than mentioned. Security vs easy of use is very broad topic, and there is no easy answer for that - it just depends on your requirements.


The issue was i installed node using sudo, to avoid errors when installing npm modules globally one MUST NEVER install node with sudo.

My solution was to reinstall node it this way:

Download latest stable node sources from nodejs.org #in my case node-v0.10.20.tar.gz

tar -zxf node-v0.10.20.tar.gz #uncompress sources

cd node-v0.10.20 #enter uncompressed folder

sudo chown -R $USER /usr/local

./configure --prefix=/usr/local && make && make install

One thing to note is that only taking ownership of the /usr/local folder wouldn't work in my case because node installation itself was made with sudo

Last step to install yeoman: #although at yeoman.io it says that doing "npm install -g yo" already installs bower and grunt, there are some submodules of grunt that fail, so i fixed that by installing it by itself

npm install -g bower

npm install -g grunt

npm install -g yo

npm install -g generator-angular


I solved this problem with environment variable and shell alias:

export NPM_PREFIX=$HOME/node
alias npmg="npm -g --prefix $NPM_PREFIX"

For me npm did not honor the "prefix" config setting in .npmrc.


Find the path to npm's directory:

npm config get prefix

For many systems, this will be /usr/local.

Change the owner of npm's directories to the name of the current user (your username!):

sudo chown -R $(whoami) $(npm config get prefix)/{lib/node_modules,bin,share}

This changes the permissions of the sub-folders used by npm and some other tools (lib/node_modules, bin, and share).

Here is the link for full details

https://docs.npmjs.com/getting-started/fixing-npm-permissions


According to this similar SO post: npm throws error without sudo

Looks like you might have an ownership issue with ~/.npm directory.

As with the answer in that one, try:

sudo chown -R `whoami` ~/.npm