Warning! These instructions are mainly for Ubuntu and Debian. There are probably similar ways of accomplishing the exact same thing in other distros as well.
- If you want to install node and npm for all users, even users who don't have admin privileges, first you need to install them as a root (sudo) user.
sudo apt update
sudo apt upgrade
sudo apt install curl build-essential
curl -sL https://deb.nodesource.com/setup_8.x | sudo -E bash -
sudo apt install -y nodejs
- Make sure both node and npm are functional, use the expected version, and work globally on the machine.
node -v
npm -v
sudo node -v
sudo npm -v
If for some reason you have an old version, you can use nvm (Node Version Manager) to install and use whatever version you want.
Now you can switch to any user you want to be able to use node and npm. They should already be able to use node and npm but can't install global packages. To fix this, the user needs a separate directory to store the global modules in. This needs to be configured in .npmrc
.
- We are going to change the prefix for the npm modules of the user by altering the option with the following line in the npm configuration file. This file needs to be called .npmrc and needs to be placed in the home directory of the user.
echo "prefix = ${HOME}/.npm/node_modules" >> ~/.npmrc
Adding the node modules binaries folder to your path is recommended. Add this to your shell config:
export PATH=/home/your_username/.npm/node_modules/bin:$PATH
And that's all you need to do to get all of node and npm running for any user on your machine! Enjoy :)