Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enable npm install -g for all users

Tags:

node.js

npm

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:

  1. Is this approach correct for what I am trying to achieve?
  2. Is /usr/local/lib/node_modules a good choice for the npm packages, or is there a better place?
  3. Is there a way to npm config set prefix once and for all users?
  4. If not, should I add that to the /etc/profile.d/node-path.sh file?
like image 256
Sharcoux Avatar asked Sep 20 '25 21:09

Sharcoux


1 Answers

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.

  1. This is indeed the correct way to enable npm install -g for all users and share the installed libraries

  2. /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.

  3. 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.

  4. 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
like image 78
Sharcoux Avatar answered Sep 22 '25 10:09

Sharcoux