0% found this document useful (0 votes)
62 views1 page

Install NPM Packages Globally Without Sudo On Macos and Linux

Uploaded by

ano
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
62 views1 page

Install NPM Packages Globally Without Sudo On Macos and Linux

Uploaded by

ano
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

Why GitHub?

Team Enterprise Explore Marketplace Pricing Search Sign in Sign up

sindresorhus / guides Sponsor Notifications Star 2k Fork 355

Code Issues 8 Pull requests Actions Security Insights

main guides / npm-global-without-sudo.md Go to file

Slayug Append to $PATH, not prepend, in the npm global guide on Fish shell (#57 … Latest commit 3f4ad3e on Jul 6, 2020 History

6 contributors

60 lines (37 sloc) 1.99 KB Raw Blame

Install npm packages globally without sudo on macOS and Linux


npm installs packages locally within your projects by default. You can also install packages globally (e.g. npm install -g <package> ) (useful
for command-line apps). However the downside of this is that you need to be root (or use sudo ) to be able to install globally.

Here is a way to install packages globally for a given user.

1. Create a directory for global packages

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

2. Tell npm where to store globally installed packages

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

3. Ensure npm will find installed binaries and man pages

Add the following to your .bashrc / .zshrc :

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 .

Check out npm-g_nosudo for doing the above steps automagically

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

See also: npm 's documentation on "Fixing npm permissions".

© 2021 GitHub, Inc. Terms Privacy Security Status Docs Contact GitHub Pricing API Training Blog About

You might also like