Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Global npm install location on windows?

I'm not 100% sure, but I believe I installed node v5 from the windows installer on both my home and office PCs.

On my home PC global installs happen under %APPDATA%:

(dev) go|c:\srv> which lessc c:\users\bjorn\appdata\roaming\npm\lessc c:\users\bjorn\appdata\roaming\npm\lessc.cmd 

while on my office PC, they go under program files:

(dev) go|w:\srv> which lessc c:\program files\nodejs\lessc c:\program files\nodejs\lessc.cmd 

I need to provide the full path to a number of these global tools to PyCharm's file watcher, and since the project file i shared it would make sense to not have global resources under a user folder.

Why would the global installs end up in different folders, and how can I force them to a location that is common to all team members?

like image 660
thebjorn Avatar asked Nov 20 '15 05:11

thebjorn


People also ask

Where does npm global install Windows?

Globally installed npm packages location The actual path to the npm package is under /usr/local/lib/node_modules . Any globally installed packages will end up in that path as well. On Linux computers, you'll find the global install location on /usr/local/lib/node_modules as well.

Where are npm global installs?

npm install -g pm2 - pm2 will be installed globally. It will then typically be found in /usr/local/lib/node_modules (Use npm root -g to check where.) If you're using nvm, then your global modules may be in one of several places depending on the version of node you're using at the time.

Where is npm located in Windows?

Type in npm root -g to see what the current path your npm is installed to. Next use npm config set prefix and your npm path will be changed to whatever directory you are currently on.


2 Answers

According to: https://docs.npmjs.com/files/folders

  • Local install (default): puts stuff in ./node_modules of the current package root.
  • Global install (with -g): puts stuff in /usr/local or wherever node is installed.
  • Install it locally if you're going to require() it.
  • Install it globally if you're going to run it on the command line. -> If you need both, then install it in both places, or use npm link.

prefix Configuration

The prefix config defaults to the location where node is installed. On most systems, this is /usr/local. On windows, this is the exact location of the node.exe binary.

The docs might be a little outdated, but they explain why global installs can end up in different directories:

(dev) go|c:\srv> npm config ls -l | grep prefix ; prefix = "C:\\Program Files\\nodejs" (overridden) prefix = "C:\\Users\\bjorn\\AppData\\Roaming\\npm" 

Based on the other answers, it may seem like the override is now the default location on Windows, and that I may have installed my office version prior to this override being implemented.

This also suggests a solution for getting all team members to have globals stored in the same absolute path relative to their PC, i.e. (run as Administrator):
(Run this in cmd, not in PowerShell!)

mkdir %PROGRAMDATA%\npm setx PATH "%PROGRAMDATA%\npm;%PATH%" /M npm config set prefix %PROGRAMDATA%\npm 

open a new cmd.exe window and reinstall all global packages.

Explanation (by lineno.):

  1. Create a folder in a sensible location to hold the globals (Microsoft is adamant that you shouldn't write to ProgramFiles, so %PROGRAMDATA% seems like the next logical place.
  2. The directory needs to be on the path, so use setx .. /M to set the system path (under HKEY_LOCAL_MACHINE). This is what requires you to run this in a shell with administrator permissions.
  3. Tell npm to use this new path. (Note: folder isn't visible in %PATH% in this shell, so you must open a new window).
like image 132
thebjorn Avatar answered Oct 21 '22 09:10

thebjorn


These are typical npm paths if you install a package globally:

Windows XP -             %USERPROFILE%\Application Data\npm\node_modules Newer Windows Versions - %AppData%\npm\node_modules or -                     %AppData%\roaming\npm\node_modules 
like image 42
Dennis Stücken Avatar answered Oct 21 '22 10:10

Dennis Stücken