# Global vs Local Installs

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

Subject: Package Managers | Lesson 10 of 24 | 3 min read
Interactive lesson: https://codewithanimation.in/package-managers/global-vs-local-install
Previous: [The Lockfile (package-lock.json)](https://codewithanimation.in/package-managers/package-lock-json.md) | Next: [npx (Package Runner)](https://codewithanimation.in/package-managers/npx-and-package-exec.md)

## 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).

## Common mistake: The "It works on my machine" syndrome

Relying on globally installed build tools is the #1 cause of 'It works on my machine' bugs. If a developer forgets to tell the team to `npm install -g some-tool@2.0.0`, the CI/CD pipeline will fail because the server has no idea what tools the project needs.

## Try it: 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 animation is on the lesson page and is not part of this text version: https://codewithanimation.in/package-managers/global-vs-local-install

## Check yourself

### Question 1

Where does a typical local npm install place project packages?

- A. Always in every other project simultaneously
- B. Only on the remote registry server
- C. Inside the browser's localStorage
- D. In the project's node_modules installation

Correct answer: D

Local installs belong to the project. Global installs use npm's configured global prefix, which can be scoped to a user rather than the entire operating system.

- Why not A: A local install does not install into unrelated projects.
- Why not B: The registry supplies content; the client installs it locally.
- Why not C: Installed package files are not browser progress data.

### Question 2

Two projects require different versions of the same build tool. Which approach makes their requirements explicit?

- A. Declare each project's tool locally with its own lockfile
- B. Use whichever global version was installed most recently
- C. Delete both projects' manifests
- D. Rename both tools without recording their versions

Correct answer: A

Project declarations and lockfiles record the required tool versions. A single ambient global tool should not be the unrecorded dependency of both builds.

- Why not B: That hides each project's version requirement.
- Why not C: Removing declarations makes the requirements less explicit.
- Why not D: Renaming does not establish version requirements.

### Question 3

Why can an npm script call a locally installed tsc without a full path?

- A. Every local install also installs the tool globally
- B. npm adds local package binaries to the script's PATH
- C. Node compiles TypeScript merely because a script is named build
- D. The registry remotely executes tsc for the script

Correct answer: B

The script process can resolve executables from node_modules/.bin. This does not require installing that compiler globally.

- Why not A: Local installation does not imply a global installation.
- Why not C: The command needs an actual compiler or appropriate tool.
- Why not D: The script invokes a local process.

## 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.
