161

In my windows installation PATH includes C:\Program Files\nodejs, where executable node.exe is. I'm able to launch node from the shell, as well as npm. I'd like new executables to be installed in C:\Program Files\nodejs as well, but it seems impossible to achieve.

Setting NODE_PATH and NODE_MODULES variables doesn't change anything: things are still installed in %appdata%\npm by default.

How can I change the global installation path?

gremo
  • 47,186
  • 75
  • 257
  • 421
  • 5
    Probably because gremo wants to have all nodejs related dependencies in one place. Problem is, that with updating nodejs you'd lose all installed packages. On the other hand putting npm in the user's %appdata% folder makes it inaccessible to other user's on the same machine. Think of installing it as normal user vs administrator. – Volker E. May 01 '14 at 21:57
  • Related to https://stackoverflow.com/questions/6685892/how-do-i-fix-npms-global-location – Volker E. May 01 '14 at 21:58
  • 1
    Moving out of %AppData% to a more _controlled location_ makes a lot of sense to me, since it feels quite %temp%-ish to mee. (Doing the same for Firefox profiles and a few others). However `C:\Program Files` is poised for constant admin right troubles. ➪ I'd rather move do `D:\my-repository\npm` or such. – Frank N Oct 16 '15 at 11:14
  • 2
    @WiredPrairie because install in user folders is pretty much just stupid. The whole point of 'globally' installing packages is that a system as a whole can access these packages. – Michahell Jan 29 '16 at 11:31
  • You can't add %AppData% to the user %PATH%, which makes grunt-cli really annoying to install on windows by default – monknomo Feb 11 '16 at 02:02
  • 5
    Here's a reason to do this: on my network you are not allowed to execute code out of the AppData folder because that's often how malware, especially CodeLocker, gets downloaded and run. It's an extra layer of protection against malware. Since Node installs things there I can't run node modules without moving the install location or decreasing security on my system. – Steve Hiner Jun 09 '16 at 20:09
  • Reasons why: #1 Path names can't be too long on Windows. #2 I'd like to be able to easily maintain my global packages. – jmbmage Jul 20 '16 at 17:05

14 Answers14

93

Everything you need is in the npm-folders documentation. I don't want to start my Win notebook now so I cannot verify it, but you should only change prefix to C:\Program Files\nodejs in your config file. If you want to change it globally for all users, edit the C:\Program Files\nodejs\npmrc file, otherwise create/edit C:\Users\{username}\.npmrc.

But this change will probably have some side effects, so read this discussion first. I don't think it's a good idea.

Swivel
  • 3,020
  • 26
  • 36
ivoszz
  • 4,370
  • 2
  • 27
  • 27
  • 37
    I found the `npmrc` file at `C:\path\to\nodejs\node_modules\npm\npmrc` – Volker E. May 01 '14 at 22:10
  • 4
    also change the npm cache location to a local directory using `npm config set cache --global` if you run into problems while installing modules and cache is in a shared drive. I got this error, `ENOENT: no such file or directory` when the cache was in a shared drive – redDevil Nov 05 '15 at 05:33
  • 1
    Don't forget to update the system PATH variable to point to the new npm folder. – Hayko Koryun Nov 30 '16 at 11:23
83

trying to install global packages into C:\Program Files (x86)\nodejs\ gave me Run as Administrator issues, because npm was trying to install into
C:\Program Files (x86)\nodejs\node_modules\

to resolve this, change global install directory to C:\Users\{username}\AppData\Roaming\npm:

in C:\Users\{username}\, create .npmrc file with contents:

prefix = "C:\\Users\\{username}\\AppData\\Roaming\\npm"

reference

environment
nodejs x86 installer into C:\Program Files (x86)\nodejs\ on Windows 7 Ultimate N 64-bit SP1
node --version : v0.10.28
npm --version : 1.4.10

Jake Berger
  • 5,237
  • 1
  • 28
  • 22
  • 2
    This is strange I was using npm 2.1.6, and it was installing all global modules in %appdata%/npm, now that I updated it to 2.6.1, it keeps trying (and failing) to install to c:\windows\program files\.. Do you happen to know when this behaviour changed? – WORMSS Mar 02 '15 at 13:37
  • 1
    here's some [node wiki diffs](https://github.com/npm/npm/wiki/Troubleshooting/_compare/fada1f67eab96cc9e320635bd1c7ab6c5fd26920...78b0a824fd358947748939268eac269823b69577?short_path=44dd9b8#diff-44dd9b836298bf616cf57a21c9e319ad) – Jake Berger Mar 02 '15 at 15:29
  • it seems it was to do with a different way I installed npm. Thanks for the diff though. – WORMSS Mar 09 '15 at 13:17
  • 2
    npm config set prefix C:\Users\{username}\AppData\Roaming\npm – Bernhard Döbler Oct 14 '16 at 21:07
  • @WORMSS I got this issue after upgrading from Node 12 to Node 14, using the same installation method though (msi installer, 64 bits). Never had it before, funny how old issues from pre-v1 can resurface... – user14764 Feb 24 '21 at 10:54
  • 1
    This saved my life! The `prefix` value was pointing to a different folder. Because of this, no matter how many times I install/uninstall angular cli globally, I would get this error: `'ng' is not recognized as an internal or external command`. Changing the value of the prefix variable in the `.npmrc` file to `prefix=C:\Users\admin\AppData\Roaming\npm` where `admin` is my username, fixed the issue. Thank you so much!!! – Devner Mar 05 '22 at 19:49
71

You can see my answer to this in my answer to another question.


In Windows, the global install path is actually in your user's profile directory

  • %USERPROFILE%\AppData\Roaming\npm
  • %USERPROFILE%\AppData\Roaming\npm-cache
  • WARNING: If you're doing timed events or other automation as a different user, make sure you run npm install as that user. Some modules/utilities should be installed globally.
  • INSTALLER BUGS: You may have to create these directories or add the ...\npm directory to your users path yourself.

To change the "global" location for all users to a more appropriate shared global location %ALLUSERSPROFILE%\(npm|npm-cache) (do this as an administrator):

  • create an [NODE_INSTALL_PATH]\etc\ directory
    • this is needed before you try npm config --global ... actions
  • create the global (admin) location(s) for npm modules
    • C:\ProgramData\npm-cache - npm modules will go here
    • C:\ProgramData\npm - binary scripts for globally installed modules will go here
    • C:\ProgramData\npm\node_modules - globally installed modules will go here
    • set the permissions appropriately
      • administrators: modify
      • authenticated users: read/execute
  • Set global configuration settings (Administrator Command Prompt)
    • npm config --global set prefix "C:\ProgramData\npm"
    • npm config --global set cache "C:\ProgramData\npm-cache"
  • Add C:\ProgramData\npm to your System's Path environment variable

If you want to change your user's "global" location to %LOCALAPPDATA%\(npm|npm-cache) path instead:

  • Create the necessary directories
    • C:\Users\YOURNAME\AppData\Local\npm-cache - npm modules will go here
    • C:\Users\YOURNAME\AppData\Local\npm - binary scripts for installed modules will go here
    • C:\Users\YOURNAME\AppData\Local\npm\node_modules - globally installed modules will go here
  • Configure npm
    • npm config set prefix "C:\Users\YOURNAME\AppData\Local\npm"
    • npm config set cache "C:\Users\YOURNAME\AppData\Local\npm-cache"
  • Add the new npm path to your environment's PATH.
    • setx PATH "%PATH%;C:\Users\YOURNAME\AppData\Local\npm"
Community
  • 1
  • 1
Tracker1
  • 19,103
  • 12
  • 80
  • 106
  • 1
    Setting the global location to C:\ProgramData as per your middle suggestion seems to be the best place for this in a windows environment, but I don't see how you can set the prefix in the global config file when the prefix is used to find the global config file. I achieved the same result by editing the builtin config file C:\Program Files\nodejs\node_modules\npm\npmrc but I suspect this change may get overwritten by an update at some stage. – oenpelli Nov 19 '14 at 06:03
  • @oenpelli In this case "global" is your user's `.npmrc` file in the home/profile directory. It would need to be set for all users. – Tracker1 Dec 15 '15 at 04:00
  • If I have no admin permission on windows, how can I change the global installation? – liam xu Mar 04 '16 at 05:50
  • liam, you can adjust this for your user via an `.npmrc` file in your profile directory... though, ymmv at that point. – Tracker1 Mar 08 '16 at 17:19
22

Building on the installation concept of chocolatey and the idea suggested by @Tracker, what worked for me was to do the following and all users on windows were then happy working with nodejs and npm.

Choose C:\ProgramData\nodejs as installation directory for nodejs and install nodejs with any user that is a member of the administrator group.

This can be done with chocolatey as: choco install nodejs.install -ia "'INSTALLDIR=C:\ProgramData\nodejs'"

Then create a folder called npm-cache at the root of the installation directory, which after following above would be C:\ProgramData\nodejs\npm-cache.

Create a folder called etc at the root of the installation directory, which after following above would be C:\ProgramData\nodejs\etc.

Set NODE environment variable as C:\ProgramData\nodejs.

Set NODE_PATH environment variable as C:\ProgramData\nodejs\node_modules.

Ensure %NODE% environment variable previously created above is added (or its path) is added to %PATH% environment variable.

Edit %NODE_PATH%\npm\npmrc with the following content prefix=C:\ProgramData\nodejs

From command prompt, set the global config like so...

npm config --global set prefix "C:\ProgramData\nodejs"

npm config --global set cache "C:\ProgramData\nodejs\npm-cache"

It is important the steps above are carried out preferably in sequence and before updating npm (npm -g install npm@latest) or attempting to install any npm module.

Performing the above steps helped us running nodejs as system wide installation, easily available to all users with proper permissions. Each user can then run node and npm as required.

gls123
  • 5,467
  • 2
  • 28
  • 28
Damilola
  • 1,014
  • 2
  • 20
  • 22
  • what is the output of 'npm config list'. Also, are you running on Windows or Linux? – Damilola Apr 06 '15 at 13:27
  • Tried it on both. `npm config add prefix` works, but none of the environment variables mentioned here. I didn't want to open up another question because this question covers it, but the answers don't (besides `npm config`). – trysis Apr 06 '15 at 16:23
  • The NODE environment variable is essentially just to expose the binaries. You still need to set the prefix and cache (if not previously set) as mentioned in the procedure. – Damilola Apr 07 '15 at 06:40
  • Oh, thank you. That was not very clear from the answers or anywhere else on Google. I was looking for environment variables as those are easier to change programmatically, but one-liner Bash commands are OK too, I suppose. – trysis Apr 07 '15 at 11:38
  • This worked beautifully for me. Thank you for the detailed instructions! – ErnestoP Oct 22 '15 at 13:18
  • Anyone know what the etc folder is needed for? – Dan Ling Dec 14 '15 at 22:30
  • @Dan The `etc` folder is where your `.npmrc` settings would be stored. – Damilola Dec 16 '15 at 04:57
  • Also i would like to Add. if you experience issue with `node-gyp` installing it's headers files in undesired directory for example by defaylt it's `UserDir/.node-gyp` in case it contains non asci chars in username for example it will not build a lib for you. To solve it just add `npm_config_devdir` environment variable in with desired path for example `C:\ProgramData\nodejs\.node-gyp` also create this directory before setting environment variable and installation of node headers via `node-gyp install node@9.6.1` for example – Mikhail.root Feb 23 '18 at 18:42
  • 1
    in windows system program data, user folder folders are places you don't want to touch, their location has been changed at least 3 times during history of windows and each time broke a few application. A better location would be another drive which most probably is in a separate M2 key or SSD than your OS drive. In a windows server it is easier to configure security of a single folder than multiple locations including program data or user folder – AaA Jun 21 '19 at 07:35
21
  • Step 1:

    npm config get prefix

    • Default Path is : %USERPROFILE%\AppData\Roaming\npm
  • Step 2:

    npm config get cache

    • Default Path is : %USERPROFILE%\AppData\Roaming\npm-cache
  • Step 3:

    npm config set prefix \npm

    • example npm config set prefix C:\\dev\\node\\npm
  • Step 4:

    npm config set cache \npm-cache

    • example npm config set cache C:\\dev\\node\\npm-cache

Run steps 1 & 2 again to check whether the paths are updated as required

That's it. Whenever you install global packages you should see them installed inside \npm\node_modules

dipenparmar12
  • 3,042
  • 1
  • 29
  • 39
Kandeepa
  • 221
  • 2
  • 2
  • Thanks your solution solved my issue, from last 2 weeks I was trying to solve the issue, Actually my windows user path had spaces in it like ABC XYZ WJS so then I changed the path to just one word, after that My npm install was giving errors, so your solution help me to clear the cache and set cache to my new location. – Tahir Afridi Nov 25 '19 at 17:05
16

You should use this command to set the global installation flocation of npm packages

(git bash) npm config --global set prefix </path/you/want/to/use>/npm

(cmd/git-cmd) npm config --global set prefix <drive:\path\you\want\to\use>\npm

You may also consider the npm-cache location right next to it. (as would be in a normal nodejs installation on windows)

(git bash) npm config --global set cache </path/you/want/to/use>/npm-cache

(cmd/git-cmd) npm config --global set cache <drive:\path\you\want\to\use>\npm-cache

davejoem
  • 4,902
  • 4
  • 22
  • 31
  • 1
    Glad to help. It would also be a great idea to make a shel script or batch file to execute this up for you whenever you change your environment. At least that's what id do – davejoem Mar 21 '17 at 01:14
  • Also worth adding " if there are spaces in the path. e.g. `"C:\Program Files\blah"`. – taylorswiftfan Apr 24 '19 at 01:36
4

The default global folder is C:\Users\{username}\AppData\Roaming\npm. You can create (if it doesn't exist) a .npmrc file in C:\Users\{username}\ and add prefix = "path\\to\\yourglobalfolder". Note that, in windows, the path should be separated by double back-slash.

angeldsWang
  • 123
  • 1
  • 7
4

Using a Windows symbolic link from the C:\Users{username}\AppData\Roaming\npm and C:\Users{username}\AppData\Roaming\npm-cache paths to the destination worked great for me.

How to add a symbolic link

enter image description here

dynamiclynk
  • 2,275
  • 27
  • 31
2

Find the current path of your global node package installation by following command.

npm list -g --depth=0

Change this path to correct path by following command.

npm set prefix C:\Users\username(Number)\AppData\Roaming\npm\node_modules

It worked for me. Read my previous answer for better understanding.

1

In Windows, if you want to move the npm or nodejs folder in disk C to another location, but it still makes sure node and npm works well, you can create symlink like this: Open Command Prompt:

mklink /D "your_location_want_to_create_symlink" "location_of_node_npm_file"

Example:

mklink /D "C:\Users\MyUser\AppData\Roaming\npm" "D:\Nodejs Data\npm"

Now you've created a symlink for npm folder, this symlink will refer to D:\Nodejs Data\npmEverything will work well.

Vinh Trieu
  • 993
  • 8
  • 12
0

I tried most of the answers here nothing seems to work in my case. So i changed the Temp location in my env variables to C:\npm. Then it started to work. This is not a good idea but a temporary solution.

Manoj
  • 497
  • 7
  • 13
0

Delete node folder completely from program file folder. Uninstall node.js and then reinstall it. change Path of environment variable PATH. delete .npmrc file from C:\users\yourusername

Santosh Kadam
  • 1,265
  • 14
  • 14
  • 2
    Have you any documentation or sources that support your statement? As written in it's current state, it's hard to tell if your suggestion actually works without trying it by yourself, which takes up time that could be saved otherwise. – Filnor Feb 05 '18 at 11:38
0

it does not require much configurations just go to advanced system settings copy the path where you have installed your node and just create an environment variable and check with node -v command in your prompt!

0

To Change the default global installation directory for node.js(npm) modules in Windows, You need to fix 2 paths.

First check the current path where node modules are installing, when you try to install them globally by using following command :

npm list -g --depth=0

It will return you the current path where node modules are installing right now. Example: D:\vsc\typescript

Now visit the following path to see npm and npm-cache folder. C:\Users\username(OR Number)\AppData\Roaming

In Roaming folder of your C drive you will find npm and npm-cache folder. Click on the + npm + folder and select the path[Just click on the bar path will be selected automatically, copy it ].This is a path image which you need to select

Once you copy this path set this to the environment variables, with a variable name Path(anything you can select as a name). Now you have set the path for your npm folder, now this is the time to set prefix. Go inside npm folder and check if node_module folder exist, if not create a new folder named as node_modules. You all global modules will come in this folder after completing all steps.

Final Step: Go to CMD and right the following command:

npm set prefix C:\Users\username(Number)\AppData\Roaming\npm\node_modules

Again this is the same path we selected for environment variable, we are just adding one more folder in the path and that is node_module.

All Set....Try Now...It will work..