Where does npm install packages?
Local Installation (Default)
- Inside the Project Folder: When you run
npm install <package>
without the-g
flag, npm installs the package in anode_modules
folder inside your current working directory (or nearest parent directory containing apackage.json
file). - 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.json
Reference: If you have apackage.json
file, the package is also added to eitherdependencies
ordevDependencies
(unless you use--no-save
or 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_modules
or$HOME/.nvm/versions/node/<version>/lib/node_modules
if 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_modules
within 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