Skip to content

Instantly share code, notes, and snippets.

@mikaelhadler
Created April 22, 2019 13:28
Show Gist options
  • Save mikaelhadler/f371612368b53b51c5a94215dafc335c to your computer and use it in GitHub Desktop.
Save mikaelhadler/f371612368b53b51c5a94215dafc335c to your computer and use it in GitHub Desktop.
Use npm without root or sudo rights on Linux

Today we're going to setup our npm installation to be used without root or sudo rights.

By default, if you install packages globally with npm, npm tries to install them into a system directory (i.e. /usr/local/lib/node_modules). With the next steps, we're going to use your home directory for those files.

First we create a directory in your home directory where all the npm packages will be installed.

$ mkdir ~/.node

Tell npm where to store the globally installed packages by adding the following line to your ~/.npmrc file.

prefix = ~/.node  

Add the new bin and node_modules folders to your $PATH and $NODE_PATH variables by adding the following lines to your ~/.profile file.

PATH="$HOME/.node/bin:$PATH"  
NODE_PATH="$HOME/.node/lib/node_modules:$NODE_PATH"  

Optionally update the $MANPATH variable for npm packages which ship man pages. Add the following line to your ~/.profile file.

MANPATH="$HOME/.node/share/man:$MANPATH"  

Source the ~/.profile and you should be able to install and use npm packages globally with your user even without root access.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment