Skip to content
Home » Install npm packages globally without sudo access in Mac

Install npm packages globally without sudo access in Mac

Having your pc organised by your company or by any means if you don’t have admin access of your piece is going to trouble you very often because of limited access. One of the challenges is the installation of npm packages globally.

When we launch an npm installation, we do it with “npm I <package>”, this will install the package into your project directory locally. But, when we run “npm I -g <package>”, this will throw access related errors. To install npm packages globally without sudo access in Mac, let’s see a way around.

Create a directory to store global packages.

mkdir "${HOME}/.npm-packages"

This command will create a directory .npm-packages in your “/Users/<userName>”. Once it is done, we need to tell npm to store global packages in this directory. Next step for npm without sudo will be

Tell npm where to store globally installed packages.

npm config set prefix "${HOME}/.npm-packages"

Now that we are done with npm configs, we need to make sure npm will find installed binaries and man pages. To do the same, add following exports to your .bashrc or .zshrc depending on what kind of terminal you are using.

Make sure npm will find binaries and man pages.

#create a varialble to store directory we created in first step.
NPM_PACKAGES="${HOME}/.npm-packages"
#export the path, so it is available globally in terminal
export PATH="$PATH:$NPM_PACKAGES/bin"
export MANPATH="${MANPATH-$(manpath)}:$NPM_PACKAGES/share/man"

If you are using bash, it may happen that .bashrc file does not yet exist and your terminal is taking env variables from .profile or .bash_profile as these files resides in user’s home directory. For this, we need to load .bashrc in terminal with following command.

source .bashrc into terminal

source ~/.bashrc

That’s it, now you can install npm packages globally without sudo access in Mac.

Leave a Reply

Your email address will not be published. Required fields are marked *