Skip to content

Instantly share code, notes, and snippets.

@sunr00t
Created November 10, 2020 20:47
Show Gist options
  • Save sunr00t/6506837a61131ed10d516efe95ddf397 to your computer and use it in GitHub Desktop.
Save sunr00t/6506837a61131ed10d516efe95ddf397 to your computer and use it in GitHub Desktop.
npm without sudo on linux

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"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment