How does npx differ from npm?
The difference is in purpose:
- npm is a package manager. It installs, updates, and removes dependencies, and runs scripts from
package.json. - npx is a package runner. It runs npm packages without requiring an install, or looks up and runs local binaries in
node_modules/.bin.
The practical difference
| Action | npm | npx |
|---|---|---|
| Install, then run | required | not required |
| Run a local binary | indirectly (via npm run) | directly |
| Run a one-off CLI command | inconvenient | convenient and fast |
Example of the difference
With npm:
bash
npm install create-react-app -g
create-react-app my-appWith npx:
bash
npx create-react-app my-appThe second option does not install the package permanently, it is downloaded to a temporary directory, run, and removed.
Summary
- npm installs packages.
- npx runs packages.
That is the key difference.
Short Answer
Interview readyPremium
A concise answer to help you respond confidently on this topic during an interview.