Skip to content

Instantly share code, notes, and snippets.

@MajorBreakfast
Last active April 4, 2017 19:37
Show Gist options
  • Save MajorBreakfast/2c9d0301f7b8a4046d0a to your computer and use it in GitHub Desktop.
Save MajorBreakfast/2c9d0301f7b8a4046d0a to your computer and use it in GitHub Desktop.
Change npm global install folder

How to change npm's global install folder

It can happen that npm install -g <package-name> reports an EPERM error. This means that npm isn't allowed to write to the directory without administrator rights. To fix this npm needs to be configured so that it uses a different folder.

Windows

1. Change npm config

npm config set prefix "%AppData%/npm"

2. Add the folder to PATH

Either use Windows' control panel to do this or the following script which changes the PATH for the current user (No admin rights required).

node windows-add-to-path.js

Hint: Don't use the setx command. It has a 1024 characters limit.

3. Logout

Log out and back in from your Windows user account.

4. Report back

If you have a github account, you should leave a comment and report back (Also if everything worked).

var execSync = require('child_process').execSync
var paths = []
try {
paths = execSync('reg query HKCU\\Environment /v PATH')
.toString().split('\r\n')[2].split(' ')[3].split(';')
} catch (e) {}
paths.push('%AppData%\\npm')
execSync('reg add HKCU\\Environment /v PATH /t REG_SZ /d "' +
paths.join(';') + '" /f')
@Hum4n01d
Copy link

Hum4n01d commented Apr 4, 2017

How would I do this on Mac?

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