What is NPX?
You'll learn what NPX is and how it lets you run package binaries without installing them globally.
The Problem NPX Solves
When you need to run a CLI tool from an npm package, you traditionally had two options: install it globally with npm install -g (cluttering your system) or install it locally and hunt for the binary deep inside node_modules/.bin/. Both approaches have drawbacks—global installs can cause version conflicts between projects, and local binaries are cumbersome to execute.
What NPX Does
NPX is a package runner that comes bundled with npm (version 5.2+). It executes binaries from npm packages without requiring permanent installation. Think of it like a rental car service: instead of buying a vehicle you'll rarely use, you rent one exactly when needed, then return it.
When you run npx <package>, NPX:
- Checks if the package exists locally in
node_modules/.bin/ - If not found locally, downloads it temporarily
- Executes the binary
- Cleans up the temporary installation
Common Use Cases
NPX shines for one-time commands or version-specific executions:
- Running project scaffolding tools:
npx create-react-app my-app - Trying packages before installing:
npx cowsay "Hello" - Executing specific versions:
npx uglify-js@3.14.0 bundle.js - Running local project scripts without path hassles
The Convenience Factor
Instead of typing ./node_modules/.bin/eslint, you simply use npx eslint. It finds and runs the locally installed version if available, ensuring you're using the project-specific tool version without global installation pollution.
Key Takeaway: NPX is npm's package runner that executes binaries on-demand, eliminating the need for global installations and making it trivial to run CLI tools from any npm package.