Skip to content
Tutorials Center
My learning

Topics

Lesson 14 of 36 ยท Installing and Versions

Global Installs and npx

Learn when to install packages globally, where global packages live, how to fix EACCES permission errors, and how npx, yarn dlx and pnpm dlx run command line tools without installing them.

Most packages are libraries you import into a project. Some are command line tools: formatters, project generators, deployment CLIs. For those, you need the command available in your terminal, either installed once for your whole computer (global) or run on demand (npx). This lesson explains both, and why running on demand is usually the better default.

Global installs

Terminal
npm install --global typescript     # short: npm i -g typescript
yarn global add typescript          # Yarn Classic
pnpm add --global typescript

After this, the tsc command works from any folder. The files live in a global directory, not in a project. Useful commands:

Terminal
npm list -g --depth=0     # what is installed globally
npm uninstall -g typescript
npm root -g               # where global packages live
npm config get prefix     # the prefix folder; binaries are linked in its bin/

The prefix's bin folder must be on your PATH for commands to be found. If a global command says "not found" after install, that folder is missing from PATH.

Why global installs are a mixed blessing

  • Version drift: your teammate has [email protected] globally and you have 5.9. The same command builds differently.
  • Invisible dependencies: a project that needs a tool but only lists it in your head will fail on CI or a new laptop.
  • Cleanup: forgotten globals pile up.

The modern habit: install tools as devDependencies of the project, and call them from scripts ("build": "tsc"). Scripts find binaries from node_modules/.bin automatically (npm Scripts). Keep globals for tools you use across all projects and everywhere (a package manager, a deploy CLI).

Binaries and node_modules/.bin

A package that exposes commands lists them in its bin field. On install, the manager creates a link (or small wrapper) for each in node_modules/.bin:

JSON
{ "name": "datefmt", "bin": { "datefmt": "./bin/datefmt.js" } }

The script's first line should be a shebang so Unix systems know which interpreter runs it: #!/usr/bin/env node. On Windows, npm generates .cmd wrappers automatically.

Node.js A command line tool in Node

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Node accepts the #! line as a comment, so the file also works as a normal module.

npx: run without installing

npx ships with npm. It looks for the command in the local node_modules/.bin; if it is not there, it downloads the package into a temporary cache, runs it and leaves your project alone.

Terminal
npx cowsay "hello"                  # fetch, run, forget
npx prettier --check .              # uses the local prettier if installed
npx create-vite@latest my-app       # project generators
npx tsc@5 --version

The equivalents are yarn dlx <pkg> (Yarn Berry), pnpm dlx <pkg> and pnpm exec <bin> / yarn <bin> for locally installed binaries.

Node.js How npx decides what to run (stand-in)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

npx runs code from the internet with your user's permissions. Check the package name carefully (typosquatting!) before confirming the "Need to install the following packages" prompt. Prefer official generators such as npm create vite@latest, which is shorthand for npx create-vite@latest.

npm create and npm init <pkg>

npm create vite@latest my-app and npm init vite my-app run the package named create-vite. The convention create-* is used by project starters, so pnpm create next-app runs create-next-app and so on.

Fixing EACCES permission errors

If npm install -g fails with EACCES: permission denied, npm tried to write to a system folder. Do not use sudo. Better options:

  1. Use a Node version manager (nvm, fnm, Volta), which installs Node in your home folder, so no special rights are needed.
  2. Or change the global prefix: npm config set prefix ~/.npm-global, then add ~/.npm-global/bin to your PATH.

Choosing between the options

Node.js Global, local or npx?

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Why prefer a devDependency over a global install for a build tool?

    Project-local versions make builds reproducible across machines.
  2. 2. What does npx do when the command is not installed?

    npx fetches, runs and leaves your project untouched.
  3. 3. What is the right response to EACCES on a global install?

    Avoid sudo; own the directory npm writes to.
  4. 4. What does #!/usr/bin/env node do in a bin script?

    The shebang names the interpreter for executable scripts.

Summary

  • Global installs put commands on your PATH for every folder; use them sparingly.
  • Project tools belong in devDependencies, called through scripts.
  • bin entries link into node_modules/.bin; start scripts with a shebang.
  • npx, yarn dlx and pnpm dlx run tools on demand; verify package names.
  • Fix EACCES with a version manager or your own prefix, never with sudo.

Next: npm Scripts