Change default global installation directory for node.js modules in Windows?

前端 未结 14 1450
遇见更好的自我
遇见更好的自我 2020-11-28 01:02

In my windows installation PATH includes C:\\Program Files\\nodejs, where executable node.exe is. I\'m able to launch node

相关标签:
14条回答
  • 2020-11-28 01:10

    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..

    0 讨论(0)
  • 2020-11-28 01:11

    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.

    0 讨论(0)
  • 2020-11-28 01:12

    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.

    0 讨论(0)
  • 2020-11-28 01:14
    • 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

    0 讨论(0)
  • 2020-11-28 01:20

    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.

    0 讨论(0)
  • 2020-11-28 01:21

    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

    0 讨论(0)
提交回复
热议问题