Nodejs / npm install global packages as normal user
This guide allows to set up npm/node so that npm install -g can be run as a normal user. This is useful to install command-line tools that should be available across multiple projects.
Based on this stackoverflow answer.
mkdir ~/.npm-packagesto create a directory to put global packages- Create
~/.npmrcwith contentprefix=~/.npm-packages - Add to
~/.bashrc:NPM_PACKAGES="~/.npm-packages"NODE_PATH="${NPM_PACKAGES}/lib/node_modules:${NODE_PATH}"PATH="${NPM_PACKAGES}/bin:${PATH}"if command -v manpath > /dev/null ; then MANPATH="$(manpath)" ; fi. Put this before any$MANPATHmodificationMANPATH="${NPM_PACKAGES}/share/man:${MANPATH}"
Install more recent version of node and npm
Once the above set up is done, it can be used to install a more recent version of node and npm for the current user, compared to what is available on the system (e.g. from official repository).
- Do the steps above
- Reload bash configuration (
source ~/.bashrc) or open a new terminal - Make sure that some version of node/npm is available on the system:
- Option 1: install from official repositories
- Option 2: download from official website and copy files where appropriate
npm install -g npm@latestnpm install -g nN_PREFIX=~/.node n {version to install (e.g. lts)}- Add installed version to a folder that is in
$PATH, e.g.ln -s ~/.node/bin/node ~/.local/bin/ - Reload bash configuration (
source ~/.bashrc) or open a new terminal - (Optional) Remove system-installed version