Mac – Install NPM / Nodejs without sudo or root
I don’t like to use sudo/root while installing third party softwares. I ran into issues while installing nodejs. Then I found a nice article which takes cares of this issue.
npm installs packages locally within your projects by default. You can also install packages globally (e.g. npm install -g ) (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.
Here is a way to install packages globally for a given user.
1. Create a directory for your global packages
mkdir “${HOME}/.npm-packages”
2. Reference this directory for future usage in your .bashrc/.zshrc:
NPM_PACKAGES=”${HOME}/.npm-packages”
3. Indicate to npm where to store your globally installed package. In your $HOME/.npmrc file add:
prefix=${HOME}/.npm-packages
4. Ensure node will find them. Add the following to your .bashrc/.bash_profile:
NODE_PATH=”$NPM_PACKAGES/lib/node_modules:$NODE_PATH”
5. Ensure you’ll find installed binaries and man pages. Add the following to your .bashrc/.zshrc:
PATH=”$NPM_PACKAGES/bin:$PATH”
unset MANPATH # delete if you already modified MANPATH elsewhere in your config
MANPATH=”$NPM_PACKAGES/share/man:$(manpath)”source ~/.bashrc
Source: Original Article