A quick reference for common npm and npx commands.
npm -v # Show npm version
node -v # Show Node.js version
npm 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.json
npm install -g <package> # Install globally
npm list --depth=0 # List top-level local packages
npm list -g --depth=0 # List top-level global packages
npm outdated # Show outdated local packages
npm update # Update local packages
npm outdated -g --depth=0 # Show outdated global packages
npm update -g # Update all global packages
npm install -g <package> # Update a specific global package
npm uninstall <package> # Remove from node_modules
npm uninstall <package> --save # Also remove from dependencies
npm uninstall <package> --save-dev # Also remove from devDependencies
npm uninstall -g <package> # Remove a global package
npm view <package> # Show package details
npm view <package> version # Show latest version
npm view <package> versions # Show all published versions
ncu
(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 versions
npm root -g # Show global node_modules path
npm prefix -g # Show global install prefix
npx
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,
npx
will use that version. - If not installed,
npx
will 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
npx
for one-off tools or project generators. - Use
npm install -g
for tools you run frequently and want available everywhere.