NPX vs Global Install
What you'll learn: When to use npx for one-time commands versus globally installing packages you'll run repeatedly.
Two Ways to Use CLI Tools
When you need a command-line tool from npm (like create-react-app or eslint), you have two options:
Global Install: Download and store the tool permanently on your system with npm install -g package-name. It's always available in your terminal, like installing a desktop application.
NPX Execution: Run the tool temporarily without installing it globally using npx package-name. NPX downloads, executes, then discards (or caches) the package.
When to Use Global Install
Choose global installation when:
- You run the tool frequently (daily or weekly)
- You want instant execution without download delays
- You prefer a consistent version across all projects
- Examples:
npm,yarn,http-server, tools you use constantly
npm install -g http-server # Install once
http-server # Use anytime, instantly
When to Use NPX
Choose npx when:
- You run the tool rarely (project initialization, occasional checks)
- You want the latest version every time
- You're testing a package before committing to it
- You want project-specific versions without global clutter
- Examples:
create-react-app,degit, package generators
npx create-react-app my-app # Always gets current version
npx cowsay "Hello" # Try without installing
Think of global install like buying a kitchen appliance you use daily, while npx is like renting a specialty tool for a one-time project.
Key Takeaway: Use global install for frequently-used tools where speed matters; use npx for occasional commands, project bootstrapping, and ensuring you always run the latest version without polluting your global namespace.