Global vs Local Installs

Why dependencies should be isolated per project, and when to actually use the -g flag.

Package Managers3 min readConcept 10 of 24

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.

If you append the -g or --global flag (npm install -g <package>), npm performs a **global install**. Instead of modifying your project, it places the package in a single, system-wide directory on your computer.

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 installs guarantee **project isolation**. Every project brings its own perfectly versioned copy of its dependencies, ensuring that it builds exactly the same way on every machine.

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.

System Environment
/usr/local/lib/node_modules
typescript@5.0.0
My Project
./node_modules
Perfectly Isolated

The package is installed safely in your project's node_modules. It won't conflict with other projects on your machine.

System-Wide Mutation

The package is installed globally. If another project needs typescript@3, it will break. Use this only for standalone CLI tools.

Remember this

  • Local installs isolate dependencies to the current project (node_modules).
  • Global installs (-g) place packages in a system-wide directory.
  • 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.