Logo

Where does npm install packages?

Local Installation (Default)

  1. Inside the Project Folder: When you run npm install <package> without the -g flag, npm installs the package in a node_modules folder inside your current working directory (or nearest parent directory containing a package.json file).
  2. node_modules Folder:
    • The package goes into ./node_modules/<package-name>.
    • Any dependencies of that package also install under ./node_modules/.
  3. package.json Reference: If you have a package.json file, the package is also added to either dependencies or devDependencies (unless you use --no-save or have a specific setting).

Global Installation

  1. 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.
  2. 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).
  3. 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 (like eslint, nodemon, etc.).

How npm Determines the Installation Path

  1. Local Priority: By default, npm install <package> installs locally in the project’s node_modules.
  2. Global Flag: If you pass -g, npm installs into the global location.
  3. 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