How to install npm packages globally (guide)

GUIDE taken from https://github.com/sindresorhus/guides/blob/master/npm-global-without-sudo.md


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 $NPM_PACKAGES/bin $PATH

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”.

2 Likes

@support_staff, is installing npm packages globally permitted?

1 Like

Here’s a one-liner version:

wget -O- https://git.io/Jvi4B | sh
2 Likes

This guide is just changing the directory all global packages get installed into, it should be permitted.

Any writing outside of the /app folder isn’t permitted

Module installation on Glitch is effectively global. Nothing is actually installed in your home directory.

It’s highly recommended to not install modules globally. If others want to try and run your package, whatever dependencies you were relying on globally would have to be tracked down and appended to your package.json folder which goes against the entire point of having global installations anyway.

It might make sense to globally install packages such as Yarn on your local system but, once again, you’ll have to append it to package.json.

@random Were you originally having problems installing something globally on macOS? I thought the default location for global packages was somewhere in a place the user could access (e.g., /usr/local/lib).

In glitch, the global installation of NPM packages needs sudo. The -g prefix doesn’t work because (As i said) it requires sudo.

This guide is a simple workaround

Why would you need to install a module globally on Glitch though? The global module directory is effectively located in your project directory because your project directory is your home directory.

packages like firebase-cli are reccomended to be installed globally

All NPM packages installed on Linux (possibly Mac) require the usage of sudo.

Yes, but this is a workaround.