Relocate NPM packages on CentOS/Redhat

NPM by default installs packages in /usr/lib/node_modules and links binaries to /usr/bin. I’ve seen at least one system binary overwritten by something installed by NPM, so I decided to relocate everything installed by NPM to /usr/local/opt/npm. Here is the magic:

First step was to brutally remove everything node:


rpm -ev npm
rpm -qa | grep ^node | xargs rpm -ev
rm -rf /usr/lib/node_modules

At this point, you’d probably want to look in /usr/bin for symbolic links pointing to /usr/lib/node_modules/bin and remove them.

Now reinstall Node and NPM. Thanks to dependencies, installing NPM is usually enough:


yum install -y npm

Now tell NPM to relocate:


npm config -g set prefix /usr/local/opt/npm

And tell the various shells to add the binary path:


echo "PATH=\$PATH:$( npm config -g get prefix )/bin" > /etc/profile.d/npm-relocate.sh

Now install something to see if it works:

npm install -g bower typescript tsd

Test:

source /etc/profile.d/npm-relocate.sh
which bower

Posted Friday, May 30th, 2014 under Uncategorized.

Leave a Reply

You must be logged in to post a comment.