What is npm install -g and how to use it effectively

Explore what npm install -g does, when to apply global installs, and best practices for managing Node.js CLI tools. This guide explains global vs local installs, PATH implications, and practical examples.

Install Manual
Install Manual Team
·5 min read
npm install -g

npm install -g is a command that installs Node.js packages globally on your system, making them accessible from any project. It uses the npm registry and places executables in your system's PATH.

Global installs in npm make tools available anywhere on your computer. The command npm install -g installs a package globally, rather than in a single project. Use it for CLI utilities you want to run from any directory, but be mindful of conflicts and permissions.

Why global installs matter

Global installation of Node.js CLI tools matters because it lets you run commands from any folder, reducing the friction of switching between projects. According to Install Manual, npm install -g is the standard approach to place executables in your system PATH, so you don’t have to install the tool in every project. This is especially useful for utilities like ESLint, TypeScript’s tsc, npm init scaffolding tools, and other command line interfaces you use across multiple repos. When a tool is installed globally, you can call it simply by its binary name in any terminal session, regardless of your current working directory. However, global installs come with tradeoffs: they can conflict with project scoped versions, complicate version management when different projects require different tool versions, and they may require elevated permissions to complete the installation. By recognizing these tradeoffs, you can decide when a global install improves your workflow and when a local installation would be safer. The goal is to simplify repetitive tasks without sacrificing project isolation or compatibility.

How npm install -g works behind the scenes

The npm install -g command tells npm to install the package globally. On most systems this means npm writes the package to a global prefix directory and symlinks the executable scripts into a directory that is on your PATH. When you run a globally installed CLI, your shell searches the PATH and launches the tool from that location. npm handles dependencies and scripts the same way as a local install, but the scope is system wide rather than project specific. Because global installations affect every project you work on, version control becomes more important: you want to avoid surprising upgrades in active workflows. To minimize surprises, use global installs for tools whose behavior remains stable across projects, and rely on project level installations for libraries whose versions matter for a specific codebase. Remember that a global install is not a replacement for a well managed package.json and npm scripts within a project.

Global vs local installs: when to choose each

Local (project level) installs live in the project's node_modules folder and are recorded in package.json. They ensure that every contributor uses the same version of a tool when they install the project dependencies. Global installs are ideal for CLIs that you expect to reuse across many projects or for init scripts you run on a machine. To decide, consider how portable you need the tool to be and who needs access to it. A common pattern is to install the CLI globally if you use it across multiple projects, and keep a minimal global footprint so you can always upgrade or reinstall without breaking a specific repo. If a tool is already defined in your project’s package.json, use npx or npm run commands to invoke the local version instead of relying on a global one.

Step by step: installing a CLI globally

  1. Verify that Node.js and npm are installed and up to date. 2) Open a terminal and run npm install -g <tool-name>, for example npm install -g eslint. 3) Confirm the installation by typing the tool name in your shell to see the CLI respond. 4) If you encounter permission errors, grant elevated privileges: on Unix like systems use sudo, on Windows run the shell as administrator. 5) To reduce future conflicts, consider using a Node version manager such as nvm or volta so you can manage multiple toolchains without polluting the global namespace. 6) Keep a short list of globally installed tools and review them periodically to remove ones you no longer use.

Troubleshooting common issues with npm -g

Global installations can fail for various reasons, including missing compiler dependencies, network issues, or insufficient permissions. Start by checking your npm version and your PATH entries. If npm cannot write to the global prefix, adjust permissions or use a version manager that isolates global directories. Another common problem is version drift, where the globally installed tool is out of date relative to a project’s requirement. In that case, prefer local installations for the project or use npm exec, npx, or package.json scripts to ensure the right version runs in a given context. If you rely on Windows, ensure the npm global bin directory is added to your PATH and that your antivirus software is not blocking scripts.

Windows vs Unix differences and best practices

Path handling and permissions vary between Windows and Unix based systems. On Windows you may need to run the terminal as administrator and ensure the npm global bin directory is in PATH. On Unix like systems you often use sudo for global installs or configure a local npm prefix under your home directory to avoid system wide changes. A best practice is to create a dedicated directory for global npm packages, configure PATH, and use a Node version manager to minimize cross project conflicts. Finally document your setup in your project guidelines so teammates understand how global tools are intended to be used within your environment.

Got Questions?

What does the -g flag do in npm install?

The -g flag installs the package globally, making the CLI available from any directory. This affects your user account and PATH, so the tool can be run anywhere. Use it for utilities you rely on across projects.

The -g flag installs a tool globally so you can run it from anywhere on your system.

When should I use npm install -g?

Use global installs for CLI tools you run across multiple projects or during setup scripts. If a tool is required by a single project, prefer a local install to keep dependencies isolated.

Use global installs for tools you need across many projects; otherwise, stick to local installs.

Can I install globally without admin rights?

Global installs usually require elevated permissions. On Unix-like systems, you may need to use sudo; on Windows, run the terminal as administrator. If admin rights are unavailable, consider a local install or a per-user prefix.

Usually you need admin rights; try administrator mode or use a per-user setup if available.

How can I check what is installed globally?

Run npm list -g --depth=0 to see top level global packages. You can also inspect the global bin directory to verify available executables.

Use npm list -g --depth=0 to see what is installed globally.

How do I uninstall a globally installed package?

Use npm uninstall -g <package>. This removes the global tool from your system while keeping your local projects unaffected.

Run npm uninstall -g package to remove a global tool.

What is the difference between npm install -g and npx?

npm install -g installs tools globally for repeated use. npx runs a package without a permanent installation, ensuring you execute the version defined by the project or the latest one if not.

Global installs are permanent; npx runs on demand without keeping the tool installed.

Main Points

  • Install globally for tools you use across multiple projects
  • Prefer local installs to avoid cross project conflicts
  • Use admin or elevated permissions only when necessary
  • Employ a Node version manager to isolate global toolchains
  • Document your global tool strategy to reduce confusion

Related Articles