Skip to content

Instantly share code, notes, and snippets.

@jftuga
Last active August 19, 2025 15:45
Show Gist options
  • Save jftuga/2922f60bc12ab245dfdf2a5e491ed501 to your computer and use it in GitHub Desktop.
Save jftuga/2922f60bc12ab245dfdf2a5e491ed501 to your computer and use it in GitHub Desktop.
npm + npx Cheat Sheet

πŸ“¦ npm + npx Cheat Sheet

A quick reference for common npm and npx commands.


πŸ” Checking Versions

npm -v          # Show npm version
node -v         # Show Node.js version

πŸ“‚ Installing Packages

Local (project-level)

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

Global

npm install -g <package>     # Install globally

πŸ“œ Listing Packages

Local

npm list --depth=0           # List top-level local packages

Global

npm list -g --depth=0        # List top-level global packages

πŸ”„ Updating Packages

Local

npm outdated                 # Show outdated local packages
npm update                   # Update local packages

Global

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

❌ Removing Packages

Local

npm uninstall <package>      # Remove from node_modules
npm uninstall <package> --save     # Also remove from dependencies
npm uninstall <package> --save-dev # Also remove from devDependencies

Global

npm uninstall -g <package>   # Remove a global package

πŸ“¦ Package Info

npm view <package>           # Show package details
npm view <package> version   # Show latest version
npm view <package> versions  # Show all published versions

πŸ›  Useful Tools

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

πŸ“ Global Install Location

npm root -g        # Show global node_modules path
npm prefix -g      # Show global install prefix

⚑ Using npx

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)

Notes:

  • 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.

βš–οΈ npm install -g vs npx

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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment