I am trying to install node in a way that make all users able to install npm packages globally without sudo access rights.
Usually, you can find on the internet people saying that you should do:
npm config set prefix $HOME/.npm-packages
However $HOME is good only for a single user.
So I went with this code:
# Install node
sudo apt install -y curl
curl -sL https://deb.nodesource.com/setup_12.x | sudo -E bash -
sudo apt install -y nodejs
# Set node global packages folder as /usr/local/lib/node_modules
NPM_PACKAGES="/usr/local/lib/node_modules"
sudo mkdir -p $NPM_PACKAGES
sudo chmod 777 $NPM_PACKAGES
npm config set prefix $NPM_PACKAGES
# Update the path and manpath to read from npm packages
echo "export PATH="\""\$PATH:$NPM_PACKAGES/bin"\""
export MANPATH="\""\${MANPATH-\$(manpath)}:$NPM_PACKAGES/share/man"\" | sudo tee '/etc/profile.d/node-path.sh'
source /etc/profile.d/node-path.sh
When I try to install pm2 with npm with another user, I get:
npm ERR! code EACCES
npm ERR! syscall symlink
npm ERR! path ../lib/node_modules/pm2/bin/pm2
npm ERR! dest /usr/bin/pm2
npm ERR! errno -13
npm ERR! Error: EACCES: permission denied, symlink '../lib/node_modules/pm2/bin/pm2' -> '/usr/bin/pm2'
Apparently, I should execute npm config set prefix $NPM_PACKAGES
for every user.
So I have 4 questions:
/usr/local/lib/node_modules
a good choice for the npm packages, or is there a better place?npm config set prefix
once and for all users?/etc/profile.d/node-path.sh
file?Ok. After all the materials I could find on the web and the expermients I could make, I reached the following conclusion. If I'm mistaken, please feel free to correct me.
This is indeed the correct way to enable npm install -g
for all users and share the installed libraries
/usr/local/lib/node_modules
is the target I found most of the time in related topics, even if it was not really about the exact same thing.
I couldn't find anything about setting npm config set prefix
for all users. There is indeed a npm config --global
command, but it didn't seem to work for this case. But maybe I missed something.
I ended up adding this line to the /etc/profile.d/node-path.sh
and it works. If someone think that it is not the correct way to do so, please comment.
In the end, this is the way I create my node-path.sh
:
# Update the path and manpath to read from npm packages
echo "export PATH="\""\$PATH:$NPM_PACKAGES/bin"\""
export MANPATH="\""\${MANPATH-\$(manpath)}:$NPM_PACKAGES/share/man"\""
npm config set prefix $NPM_PACKAGES" | sudo tee '/etc/profile.d/node-path.sh'
In conclusion, the correct way to enable ̀npm install -g` for all users seems to be:
# Install node
sudo apt install -y curl
curl -sL https://deb.nodesource.com/setup_12.x | sudo -E bash -
sudo apt install -y nodejs
# Set node global packages folder as /usr/local/lib/node_modules
NPM_PACKAGES="/usr/local/lib/node_modules"
sudo mkdir -p $NPM_PACKAGES
sudo chmod 777 $NPM_PACKAGES
# Update the path and manpath to read from npm packages
echo "export PATH="\""\$PATH:$NPM_PACKAGES/bin"\""
export MANPATH="\""\${MANPATH-\$(manpath)}:$NPM_PACKAGES/share/man"\""
npm config set prefix $NPM_PACKAGES" | sudo tee '/etc/profile.d/node-path.sh'
source /etc/profile.d/node-path.sh
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