Change default NodeJS - npm and npm-cache directory on Windows

Developers mostly like to take control of your machine and how things work on it. This time I decided to take control where NodeJS would restore my global packages and manages package cache.

In this case I am referring to Windows 10 particularly as that is my core development platform.

By default NodeJS gets installed in C:\Program Files\nodejs folder (assuming you are installing nodejs - Windows x64 version).

After installation lets say I wish to install http-server package so I can run static HTTP server from any folder.

As soon as I do npm install http-server -g, this start adding package and it's dependency packages into C:\Users\username\AppData\Roaming\npm and C:\Users\username\AppData\Roaming\npm-cache folder.

So I wish to shift these two folders to different and custom location e.g: D:\nodejs. Let's do it!

Step:
Edit NPMRC file at follow location: C:\Program Files\nodejs\node_modules\npm\npmrc
This should be containing one like for path / prefix:

prefix=${APP_DATA}\npm

Change it to new location:

prefix=D:\nodejs\npm

Step: Edit user's NPMRC file at following location: C:\Users\usernamee\.npmrc
Replace following path fragment for all cases: C:\Users\username\AppData\Roaming with D:\nodejs

Following steps will change the default path for npm folder.

Step: Run the following command at command prompt to change the default path for npm-cache

npm config set cache D:\nodejs\npm-cache --global

Step: Change the path for npm set at user environment variable level.
Open Environment variable edit dialog: Right click on Windows icon > System > Advanced system settings > Environment Variables > Top section "User variables for username.

Select Path variable and click Edit
Here edit npm default location to custom path as D:\nodejs\npm

That's it, next time when you run npm install anything -g it would restore those packages and setup npm cache at your custom location.

Happy coding!