Where does npm install packages?
Local Installation (Default)
- Inside the Project Folder: When you run
npm install <package>without the-gflag, npm installs the package in anode_modulesfolder inside your current working directory (or nearest parent directory containing apackage.jsonfile). - node_modules Folder:
- The package goes into
./node_modules/<package-name>. - Any dependencies of that package also install under
./node_modules/.
- The package goes into
package.jsonReference: If you have apackage.jsonfile, the package is also added to eitherdependenciesordevDependencies(unless you use--no-saveor have a specific setting).
Global Installation
- System-Wide Location: When you include the
-g(or--global) flag (npm install -g <package>), npm installs the package globally, typically in a system-wide directory. - Typical Global Paths:
- macOS/Linux:
/usr/local/lib/node_modulesor$HOME/.nvm/versions/node/<version>/lib/node_modulesif using nvm. - Windows:
%USERPROFILE%\AppData\Roaming\npm\node_modules(for a user-level install).
- macOS/Linux:
- Executable Binaries: Global installs often place the package’s executable scripts in a directory included in your system’s
$PATH, allowing you to run commands directly (likeeslint,nodemon, etc.).
How npm Determines the Installation Path
- Local Priority: By default,
npm install <package>installs locally in the project’snode_modules. - Global Flag: If you pass
-g, npm installs into the global location. - Config Overrides: You can override locations by editing npm’s config files or using environment variables (like
PREFIX).
Recommended Resource
Summary
- Local installs go into
node_moduleswithin your project’s directory. - Global installs go into a system-wide folder (location varies by OS, Node version manager, and environment).
- Local is the standard approach for app or library dependencies; global is used for CLIs or tools you want to use from any location.
CONTRIBUTOR
TechGrind