Learning navigation

Code with Animation

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.

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.

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.

Check yourself

Pick an answer to lock it in, then read why. Getting one wrong is part of how it sticks.

  1. 1Where does a typical local npm install place project packages?
  2. 2Two projects require different versions of the same build tool. Which approach makes their requirements explicit?
  3. 3Why can an npm script call a locally installed tsc without a full path?

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.