moss moss - 1 year ago 52
Linux Question

How do I install Node and NPM so I don't have to use sudo?

I am trying to set up Node.js and NPM on a Ubuntu 14.04 machine but am having some trouble. On my first try I kept getting

errors when trying to install packages(sometimes even with
), so I completely uninstalled node and npm. Now I'm trying to figure out how to install them in a way that won't require me to run everything
and won't give me

I'd rather not use NVM, and other than that I've just found a bunch of scattered partial answers, some of which contradict each other, or just use slightly different syntax.

I'd really appreciate some help with this. I've just made the switch over to Linux from Windows and am trying to get set up so I can start working again.

Answer Source

The problem that throws an EACCES is often a permissions issue on two folders:

There could be a permissions problem on your ~/.npm directory.

The full path to your '~/.npm' directory is '/Users/YOUR_USERNAME/.npm'; it stores various npm functionality. The easiest way to get to your home directory on a unix based system (I believe on linux as well) is to type 'cd'.

There could also a permissions problem on your /usr/local/lib/node_modules.

This is where npm tries to store your globally installed modules. It is the system level version of the 'node_modules' folder you find in basically any node.js project you make and install with dependencies.

I actually made a node package that will resolve this issue on a mac, although I'm not sure about Linux (because the paths to '.npm' and 'node_modules' may be different on linux), you could give it a shot? It basically reasserts you as the owner of those directories.

Here's the github page: (just a quick note this package might be out of date because of a weird build issue with the osenv dependency)

If you reinstall node you could try to change the permissions yourself with these commands (which should work in Linux):

sudo chown -R $(whoami) ~/.npm

sudo chown -R $(whoami) /usr/local/lib/node_modules

NOTE: I'm not 100% sure that the paths '/usr/local/lib/node_modules' and '~/.npm' are correct for a Linux based Node/npm install. Can you post the full error message you get in the terminal?