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:
.npmrc
file with root
, binroot
, and manroot
pointing to folders that the user owns. 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.
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.
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.
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.
NVM (Node Version Manager) is the best way to run multiple versions of NodeJS on the same machine.
This worked for me:
Make a ~/.node
folder
mkdir ~/.node
Edit ~/.npmrc
and add the line
prefix = ~/.node
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With