Install npm Packages Without Sudo
Posted July 24, 2022 by Rohith ‐ 1 min read
npm installs packages locally within your projects by default. You can also install packages globally (e.g. npm install -g
Below are the steps to install packages globally for a given user in Linux or Mac
- Create a directory for global packages
mkdir "${HOME}/.npm-packages"
- Tell npm where to store globally installed packages
npm config set prefix "${HOME}/.npm-packages"
- Ensure npm will find installed binaries and man pages
NPM_PACKAGES="${HOME}/.npm-packages"
export PATH="$PATH:$NPM_PACKAGES/bin"
# Preserve MANPATH if you already defined it somewhere in your config.
# Otherwise, fall back to `manpath` so we can inherit from `/etc/manpath`.
export MANPATH="${MANPATH-$(manpath)}:$NPM_PACKAGES/share/man"
If you’re using fish, add the following to ~/.config/fish/config.fish
set NPM_PACKAGES "$HOME/.npm-packages"
set PATH $PATH $NPM_PACKAGES/bin
set MANPATH $NPM_PACKAGES/share/man $MANPATH
If you have erased your MANPATH by mistake, you can restore it by running set -Ux MANPATH (manpath -g) $MANPATH once. Do not put this command in your config.fish.
Note:
If you are running macOS, the .bashrc file may not yet exist, and the terminal will be obtaining its environment parameters from another file, such as
.profile
or.bash_profile
. These files also reside in the user’s home folder. In this case, simply adding the following line to them will instruct Terminal to also load the.bashrc
file:source ~/.bashrc
Read more about startup files in linux