Global vs Local Installs
Why dependencies should be isolated per project, and when to actually use the -g flag.
Two modes of installation
By default, when you run npm install <package>, npm performs a local install. It downloads the package into the node_modules directory of your current project and adds it to your package.json.
A global install with -g or --global uses npm's configured global prefix rather than this project's normal local installation. That prefix may be user-specific; it is not necessarily a single operating-system-wide directory.
The danger of global dependencies
In the early days of Node.js, it was common to install build tools (like gulp or typescript) globally to save disk space. However, this creates a massive architectural problem: Version Conflicts.
If Project A requires typescript@3 and Project B requires typescript@5, a single global installation cannot satisfy both. One of the projects will inevitably break.
Local declarations and a committed lockfile make each project's tool requirements explicit. Consistent runtime, platform and configuration choices are also needed when reproducing a complete build.
When to use global installs
You should almost never use global installs for project dependencies or build tools.
The only valid use case for -g is installing standalone CLI tools that you use across your entire operating system, independent of any specific codebase (for example, npm-check-updates or the vercel CLI).
Install Mode Simulator
Toggle between global and local install modes below to see where npm places the files and how it affects project isolation.
The package is installed safely in your project's node_modules. It won't conflict with other projects on your machine.
The package is installed globally. If another project needs typescript@3, it will break. Use this only for standalone CLI tools.
Check yourself
Pick an answer to lock it in, then read why. Getting one wrong is part of how it sticks.
Remember this
- Local installs isolate dependencies to the current project (
node_modules). - Global installs use the configured global prefix, which may be user-specific.
- Never use global installs for project dependencies or build tools to avoid version conflicts.
- Only use global installs for standalone, system-wide CLI utilities.
Done with this concept?
Mark it complete to track your progress. No login needed.