Course:Node.js & Express/
Lesson

Before npxWhat is npx?A tool bundled with npm that runs a package binary without installing it globally, always fetching the latest version. existed, using a project scaffolder like create-react-app meant installing it globally with npm install -g, leaving a permanent entry on your machine that would slowly fall out of date. Run npx create-react-app my-app today and you get the latest version every single time, with nothing left behind when you are done.

The problem npxWhat is npx?A tool bundled with npm that runs a package binary without installing it globally, always fetching the latest version. solves

Global installs have three annoying failure modes. First, the globally installed version gets stale, you ran npm install -g create-react-app six months ago and now the scaffolder generates a project with last year's defaults. Second, different projects on the same machine might need different versions of the same CLIWhat is cli?Short for Command Line Interface. A tool you use by typing commands in the terminal instead of clicking buttons., and a global install forces you to pick one. Third, every global package is just noise on your system PATH.

npx fixes all three by following this resolution order:

  1. Look for the binaryWhat is binary?A ready-to-run file produced by the compiler. You can send it to any computer and it just works - no install needed. in ./node_modules/.bin (the local project version)
  2. If not found, download and run it temporarily from the npm registryWhat is registry?A server that stores and distributes packages or container images - npm registry for JavaScript packages, Docker Hub for container images.
  3. Clean up after execution
Because npx checks local node_modules/.bin first, running npx tsc in a project that has TypeScript installed as a devDependency will always use that project's exact version, not whatever is on your PATH.
02

Common use cases

ScaffoldingWhat is scaffolding?Auto-generating the basic file structure and starter code for a project or feature so you don't have to write it from scratch. new projects

# React (classic)
npx create-react-app my-app

# Next.js
npx create-next-app@latest my-app

# Vite
npx create-vite my-app --template react

# Express generator
npx express-generator my-api

Running development tools without installing

# Lint your project without a global ESLint
npx eslint src/

# Format with Prettier
npx prettier --write .

# Run Prisma migrations
npx prisma migrate dev

# Generate a TypeScript config
npx tsc --init

Testing a package quickly

Think of this like a try-before-you-buy. You can evaluate a utility without committing to adding it to your project:

# Spin up a quick static file server
npx http-server ./dist -p 8080

# Try lodash without installing it
npx -p lodash node -e "const _ = require('lodash'); console.log(_.chunk([1,2,3,4], 2));"
03

Pinning versions

When you need reproducible results, like in a CI pipelineWhat is pipeline?A sequence of automated steps (install, lint, test, build, deploy) that code passes through before reaching production., pin the version explicitly:

# Always latest
npx create-react-app@latest my-app

# Specific version
npx create-react-app@4.0.0 my-app
Never use an unpinned npx command in a CI script that runs automatically. Without a version pin, a package release overnight could change your build output or break the pipeline entirely.
04

npxWhat is npx?A tool bundled with npm that runs a package binary without installing it globally, always fetching the latest version. vs npm exec

npm 7 introduced npm exec as an official, semantically equivalent command. The main practical difference is argument parsing:

# These do the same thing
npx eslint src/
npm exec eslint src/

# npm exec requires -- to separate its flags from the tool's flags
npm exec -- eslint src/ --fix

Use whichever feels natural. npx is shorter and more widely recognised; npm exec is what the official npm documentation now recommends.

05

Using npxWhat is npx?A tool bundled with npm that runs a package binary without installing it globally, always fetching the latest version. inside npm scripts

Because npm scripts already know about node_modules/.bin, you often do not need npx in scripts at all. But it is valid, especially for tools you want to always fetch fresh:

json
{
  "scripts": {
    "migrate": "npx prisma migrate dev",
    "format": "npx prettier --write .",
    "init:ts": "npx tsc --init"
  }
}
06

Package binaries

Any package that declares a bin field in its package.json can be invoked through npxWhat is npx?A tool bundled with npm that runs a package binary without installing it globally, always fetching the latest version.:

json
{
  "name": "my-cli",
  "bin": {
    "my-cli": "./bin/cli.js"
  }
}
npx my-cli

This is exactly how tools like prisma, eslint, and tsc work, they are normal npm packages that happen to ship a binaryWhat is binary?A ready-to-run file produced by the compiler. You can send it to any computer and it just works - no install needed..

07

Quick reference

TaskCommand
Run a tool temporarilynpx some-tool
Pin to a specific versionnpx some-tool@1.2.3
Always fetch latestnpx some-tool@latest
Use local versionnpx tsc (reads node_modules/.bin/tsc)
npm 7+ equivalentnpm exec -- some-tool --flag
Skip confirmation promptnpx --yes some-tool