How to Use Install npm packages without sudo on Linux and MacOS
Permissions issues are frustrating. Sometimes you don’t WANT to give things administrator-level access to your computer. Or maybe you’re on someone else’s system, and you CAN’T give it administrator-level access; you don’t have that level of access either. Maybe you just don’t want to have to type ‘sudo’ first every time. All are legitimate frustrations developers have to deal with at some point.
And as any of those developers will tell you, the only thing more irritating than giving something admin access is dealing with systems with mixed permissions: some packages can be installed with only user permissions, and seemingly work just fine. That is, until you need that package for something different.
Soon you find yourself in the hell of installing, uninstalling, reinstalling, sorting through trying to figure out which packages are installed where, and cleaning up issues left behind from installing and reinstalling.
Here’s how to configure npm on your Linux or MacOS system to be permission frustration-free:
First, open a terminal and create a hiddeHere’s how to configure npm on your Linux or MacOS system to be permission frustration-free:n folder in your Home directory. This will be npm’s new default directory for global installations, we’ll call it npm-global.
cd ; mkdir .npm-global
Next, tell npm to use the new directory path as default.
npm config set prefix '~/.npm-global'
Using your favorite text editor, open or create a ~/.profile file and add this line to it
export PATH=~/.npm-global/bin:$PATH
Save and close the .profile file. Returning to your terminal, enter:
source ~/.profile
And you’re done! To test it out, trying running this to install JSHint, an open-source static analysis tool that can catch some common errors and save you tons of debugging time
npm install -g jshint