How does a local install differ from a global one?
A local install puts a package inside a specific project, in the node_modules folder next to package.json.
A global install puts a package at the system level, so it is available from any project as a CLI tool.
Local install
bash
npm install <package>- The package is stored in
./node_modules - An entry is added to
dependenciesordevDependencies - Available only inside this project
- Used for libraries the application needs at runtime or build time (React, Webpack, Axios, etc.)
Global install
bash
npm install -g <package>- The package is installed into npm's system directory
- Not available as a project dependency
- Can be invoked as a terminal command from any directory
- Usually used for CLI utilities (for example:
nodemon,npm-check,create-react-app)
Comparison
| Trait | Local | Global |
|---|---|---|
| Where it installs | Into the project (node_modules) | Into the system |
| Visibility | Only in the project | For all projects |
| Entry in package.json | Yes | No |
| What it suits | Project libraries | CLI tools |
Short summary: a local install is for project dependencies, a global install is for tools you need to run from anywhere on the system.
Short Answer
Interview readyPremium
A concise answer to help you respond confidently on this topic during an interview.