Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you set up npm (node package manager) without root access?

Setting npm up as the root user is straighforward and workds. Except you have to run npm commands as root (not recommended). So I thought I'd try setting it up as a non-root user.

According to npm documentation, a non-root user without root access can set up npm by:

  1. creating a .npmrc file with root, binroot, and manroot pointing to folders that the user owns.
  2. Then running the install script.

OK. Install was fine.

But node can't see the packages provided by npm.

So how do I make node aware of the packages provided by npm? (I didn't have to do anything when I previously installed npm as root). I can set require.paths within node, or set the NODE_PATH environment variable, but to what?

Thanks.

like image 251
Mark Bolusmjak Avatar asked Sep 15 '10 15:09

Mark Bolusmjak


People also ask

Can you install npm without root?

npm installs packages locally within your projects by default. You can also install packages globally (e.g. npm install -g <package> ) (useful for command-line apps). However the downside of this is that you need to be root (or use sudo ) to be able to install globally.

Should you run npm as root?

You should not run applications with root privileges if its not necessary. Node and npm can do their work just perfectly fine without admin powers. If you are running a server with root privileges and it gets hacked through a vulnerability in your code, the attacker will have total control over your machine.

Do I need sudo to install node?

To summarize, you can use the following steps to update your local OS X machine to allow you to use global Node. js modules and different versions of node without using sudo . brew install node , or apt-get install node or whatever you need to do to get node up on your machine.

Do you need admin rights to install NodeJS?

NVM (Node Version Manager) is the best way to run multiple versions of NodeJS on the same machine.


1 Answers

This worked for me:

  1. Make a ~/.node folder

    mkdir ~/.node
    
  2. Edit ~/.npmrc and add the line

    prefix = ~/.node
    
  3. Edit your ~/.profile or ~/.bash_profile and add these lines

    PATH="$HOME/.node/bin:$PATH"
    NODE_PATH="$HOME/.node/lib/node_modules:$NODE_PATH"
    

Now I can do things like npm -g install http-server and it will install to ~/.node without root. With this in place, when I then type http-server, it runs.

like image 76
gman Avatar answered Oct 12 '22 22:10

gman