Often we find ourselves dealing with directory permissions when trying to install global node modules, and since it's getting in the way of what we were trying to do in the first place, we often go for the brute force approach and try and chown that folder and that leads to a lot of problems latter on.
Change npm's default directory to another directory
An alternative approach is to set up npm's default directory in the users home where there won't be any permission issues and there is no need to escalate to sudo for any operations.
This can be done in the following easy steps:
- Make a directory for global installations:
mkdir ~/.npm-global`
- Configure npm to use the new directory path:
npm config set prefix '~/.npm-global
- Open or create a
~/.profile
file and add that folder to the PATH (.zshrc
if you are using zsh):
echo "export PATH=~/.npm-global/bin:$PATH" >> ~/.profile && source ~/.profile
Now when you install global modules, they will be installed in this new directory, problem solved!
© Sajeev Nair.RSS