How to fix npm missing write access on linux

When you install a package globally with npm install -g <package> without node version manager you can get Missing write access to /usr/lib/node_modules error. Following methods are the easiest ways to fix this issue.

1. Reinstall npm with a node version manager

This method is the recommended way by the npm to fix this issue. You can use nvm or n as a node version manager.

2. Change nmp’s default directory

Create a hidden folder in your home directory for npm.

1
mkdir ~/.npm-global

Configure npm to use custom location

1
npm config set prefix '~/.new-npm'

Add bin directory to PATH in ~/.profile

1
export PATH=~/.npm-global/bin:$PATH

Update system variables and you are ready to install packages globally

1
2
source ~/.profile
npm install -g <package_name>

3. Using NPM_CONFIG_PREFIX environment value

You can set a new npm directory using NPM_CONFIG_PREFIX. Add the following line to the ~/.profile file.

1
NPM_CONFIG_PREFIX=~/.new-npm

Update system variables and you know what to do.

1
2
source ~/.profile
npm install -g <package_name>