A quick reference for common npm and npx commands.
npm -v # Show npm version
node -v # Show Node.js versionnpm install <package> # Install locally and add to dependencies (default)
npm install <package>@1.2.3 # Install specific version and add to dependencies
npm install <package> --save-dev # Install and add to devDependencies
npm install <package> --no-save # Install locally but do NOT update package.jsonnpm install -g <package> # Install globallynpm list --depth=0 # List top-level local packagesnpm list -g --depth=0 # List top-level global packagesnpm outdated # Show outdated local packages
npm update # Update local packagesnpm outdated -g --depth=0 # Show outdated global packages
npm update -g # Update all global packages
npm install -g <package> # Update a specific global packagenpm uninstall <package> # Remove from node_modules
npm uninstall <package> --save # Also remove from dependencies
npm uninstall <package> --save-dev # Also remove from devDependenciesnpm uninstall -g <package> # Remove a global packagenpm view <package> # Show package details
npm view <package> version # Show latest version
npm view <package> versions # Show all published versionsncu (npm-check-updates) is useful because it quickly scans package.json and shows or upgrades all dependencies to their latest versions, saving you from manually checking each package.
npm install -g npm-check-updates # Install update helper
ncu -u # Update package.json to latest versionsnpm root -g # Show global node_modules path
npm prefix -g # Show global install prefixnpx lets you run npm packages without installing them globally.
npx <package> # Run a package temporarily
npx cowsay "Hello!" # Example: run cowsay without installing
npx create-react-app my-app # Run project generators
npx tsc # Run TypeScript compiler (if installed locally)- If the package is installed locally,
npxwill use that version. - If not installed,
npxwill download and run it temporarily. - Great for one-off commands or project scaffolding tools.
| Use Case | npm install -g |
npx |
|---|---|---|
| Frequent use | β
Best for tools you use often (e.g., typescript, eslint) |
β Not ideal (downloads each time if not installed) |
| One-time use | β Clutters global installs | β
Perfect for one-off commands (e.g., npx cowsay) |
| Always latest version | β You must manually update with npm update -g |
β Always fetches the latest version by default |
| Offline use | β Works offline once installed | β Needs internet if not installed locally |
| Disk space | β Uses space in global node_modules |
β No permanent install (unless cached) |
β Rule of Thumb:
- Use
npxfor one-off tools or project generators. - Use
npm install -gfor tools you run frequently and want available everywhere.