Installing npm packages globally using root priviledges is a bad idea for a number of reasons. Some modern distributions will fail when trying to do so (e.g. Fedora). In this blog post I will explain the necessary steps to save npm packages to your user folder instead.
Create a folder for the packages
$ mkdir ~/.npm-packages
npm to use a different path for globally installed packages
$ npm config set prefix ~/.npm-packages
Edit the configuration of your shell to make sure
npm finds the binaries and man pages. For
zsh add the following to
# Add packages to path. NPM_PACKAGES=~/.npm-packages export PATH=$NPM_PACKAGES/bin:$PATH # Add manpages. Note that the unset command is NOT necessary if you modify it somewhere else. unset MANPATH export MANPATH="$NPM_PACKAGES/share/man:$(manpath)"
In case you’re using the
fish shell you need to edit
# Add packages to path. set PATH "~/.npm-packages/bin" $PATH
You are now able to invoke
$ npm install -g gatsby-cli