Growing Wiki

How to manage NPM packages on Arch Linux

🌱 Seedling

What to avoid!

Avoid using sudo with npm to install packages as this can lead to permission issues, security risks and conflicts with the pacman package manager.

The “correct” methods

Method 1 - Use a Version Manager

The first method is to use something like fnm (Fast Node Manager) or nvm (Node Version Manager).

These will allow you to install multiple versions of Node.js for development without the use of root privileges. Everything will be contained in your $HOME directory.

So if you’re on Arch Linux, you can install the managers using pacman like:

sudo pacman -S fnm
# or pacman -S nvm

You will then have to set up your shell, in my case ~/.zshrc, like:

eval "$(fnm evn --use-on-cd --shell zsh)"

And finally, install Node:

fnm install --latest

Method 2 - Change the default directory for npm

If you prefer to use the system Node package from you package manager, in my case, sudo pacman -S nodejs npm. You can redirect global installs (=npm install -g) to your $HOME directory.

First, create a directory where global packages will be stored:

mkdir -p ~/.local/share/npm-global # Or any directory names you fancy

Then configure npm to use this new path:

npm config set prefix '~/.local/share/npm-global'

After that, you will then have to update your PATH. In my case, this would be set in ~/.zshrc:

export PATH="$HOME/.local/share/npm-global/bin:$PATH"

Before you test or use this method, you must first source the new configuration to refresh your shell:

source ~/.zshrc