npx (Package Runner)
How to execute CLI tools without polluting your system with global installs.
What is npx?
npx (Node Package eXecute) is a CLI tool that comes bundled with npm (version 5.2+). Its primary purpose is to execute Node binaries (command-line tools) easily and safely.
Before npx, if you wanted to run a tool like create-react-app or jest, you had to either install it globally (which causes version conflicts) or dig into your local node_modules/.bin directory to execute it manually.
The Power of Temporary Execution
The true power of npx is how it handles packages that are **not** installed on your machine.
If you run npx create-vite@latest, npx realizes you don't have create-vite installed. Instead of failing, it transparently downloads the package to a temporary system cache, executes the bootstrapping script, and then cleans up the cache.
This means you always run the absolute latest version of the tool without leaving junk files permanently installed on your hard drive.
The Execution Priority
When you type an npx command, it follows a strict resolution order:
1. **Local .bin**: It first checks node_modules/.bin/ in your current project. If the tool is installed locally, it runs that exact locked version.
2. **Global $PATH**: If not found locally, it checks your system's global binaries.
3. **Temporary Fetch**: If neither has it, npx reaches out to the npm registry, downloads it temporarily, runs it, and deletes it.
npx Execution Simulator
Run the simulated npx create-vite command below. Watch how npx checks local bins, fetches to a temporary cache, executes, and cleans up.
Remember this
npxexecutes Node command-line tools.- It prioritizes locally installed packages (
node_modules/.bin). - If a package is missing,
npxdownloads it temporarily, runs it, and cleans it up. - It eliminates the need for global installations of scaffolding tools (like
create-vite).
Done with this concept?
Mark it complete to track your progress. No login needed.