Skip to main content

NPM & NPX Cheatsheet

Comprehensive NPM & NPX Cheatsheet

1. NPM (Node Package Manager)

NPM is the default package manager for Node.js, used for installing, managing, and sharing JavaScript packages.

Initializing a Project

CommandDescription
npm initInitialize a new project (interactive)
npm init -yInitialize with default settings (skips prompts)
npm set init-author-name "Your Name"Set default author name
npm set init-license "MIT"Set default license

Installing Packages

CommandDescription
npm install <package>Install a package locally
npm install -g <package>Install a package globally
npm install --save <package>Install & add to dependencies (default in npm 5+)
npm install --save-dev <package>Install & add to devDependencies
npm install --save-optional <package>Install & add to optionalDependencies
npm install --no-save <package>Install without saving to package.json
npm install --forceForce reinstall all packages
npm install --productionInstall only production dependencies

Updating & Removing Packages

CommandDescription
npm updateUpdate all packages
npm update <package>Update a specific package
npm uninstall <package>Remove a package
npm uninstall -g <package>Remove a global package
npm outdatedCheck for outdated packages

Running Scripts

CommandDescription
npm run <script>Run a script defined in package.json
npm startRuns "start" script (shortcut)
npm testRuns "test" script (shortcut)
npm restartRuns "restart" script (shortcut)
npm run envList environment variables

Managing the Cache

CommandDescription
npm cache verifyVerify cache integrity
npm cache clean --forceClear npm cache

Publishing & Managing Packages

CommandDescription
npm loginLog in to npm registry
npm publishPublish a package
npm unpublish <package>@<version>Unpublish a package version
npm owner add <user> <package>Add an owner to a package
npm owner rm <user> <package>Remove an owner from a package

Miscellaneous

CommandDescription
npm lsList installed packages (local)
npm ls -gList globally installed packages
npm rootShow path to local node_modules
npm root -gShow path to global node_modules
npm config listList npm configuration
npm doctorCheck npm environment for issues
npm repo <package>Open package repository in browser
npm docs <package>Open package documentation

2. NPX (Node Package Executor)

NPX is a tool for executing Node packages without installing them globally.

Running Packages

CommandDescription
npx <package>Run a package (installs if not present)
npx <package>@<version>Run a specific version of a package
npx -p <package> <command>Install a package temporarily & run a command
npx --no-install <package>Run only if already installed

Advanced Usage

CommandDescription
npx --node-arg="--inspect" <package>Run with Node.js flags
npx -c "command1 && command2"Execute multiple commands
npx --package=<package> <command>Specify a package to use

Common Use Cases

CommandDescription
npx create-react-app my-appCreate a React app without global install
npx http-serverRun a local HTTP server
npx eslint .Run ESLint without installing globally
npx cowsay "Hello"Run a fun CLI tool

3. Useful Tips

  • Global vs Local: Prefer npx over global installs to avoid version conflicts.
  • package-lock.json: Never manually edit; it ensures consistent installs.
  • npm ci: Faster, stricter alternative to npm install for CI/CD (requires package-lock.json).
  • .npmrc: Configure npm settings per project or globally.

This cheatsheet covers the most essential npm and npx commands. Let me know if you need further details! 🚀

NPM & NPX Symbol Reference Guide

In npm and npx commands, symbols like @, ^, ~, and others have special meanings. Here's a breakdown of their usage:


1. @ (At Symbol)

The @ symbol is primarily used for:

a) Specifying Package Versions

npm install package@version
  • Example:
    npm install lodash@4.17.21
    • Installs Lodash v4.17.21 exactly.

b) Scoped Packages (Namespaced Packages)

npm install @org/package
  • Example:
    npm install @angular/cli
    • Installs the Angular CLI from the @angular scope.

c) With npx to Run Specific Versions

npx package@version
  • Example:
    npx create-react-app@5.0.1 my-app
    • Runs CRA v5.0.1 without installing it globally.

2. ^ (Caret Symbol) – Version Range (Default in package.json)

  • Allows minor and patch updates but not major updates.
  • Example:
    "dependencies": {
    "express": "^4.18.2"
    }
    • Allows updates like 4.19.0 but not 5.0.0.

3. ~ (Tilde Symbol) – Patch-Only Updates

  • Allows only patch updates (bug fixes).
  • Example:
    "dependencies": {
    "express": "~4.18.2"
    }
    • Allows 4.18.3 but not 4.19.0.

4. > (Greater Than), < (Less Than), >=, <= (Version Ranges)

  • Used to specify version constraints.
  • Examples:
    npm install "react@>17.0.0"  # Installs React above v17.0.0
    npm install "lodash@<=4.17.21" # Installs Lodash v4.17.21 or older

5. || (OR Operator for Multiple Ranges)

  • Allows multiple version ranges.
  • Example:
    npm install "react@^16.8.0 || ^17.0.0"
    • Installs either React 16.8+ or 17.0+.

6. - (Hyphen) – Version Range

  • Specifies an inclusive range.
  • Example:
    npm install "react@16.0.0 - 17.0.0"
    • Installs any React version from 16.0.0 to 17.0.0.

7. latest (Keyword for Latest Version)

  • Explicitly installs the newest version.
  • Example:
    npm install lodash@latest

8. * (Wildcard for Any Version)

  • Installs any available version (not recommended).
  • Example:
    npm install "lodash@*"

9. # (Commit Hash / Git Reference)

  • Installs from a Git commit, branch, or tag.
  • Example:
    npm install github:user/repo#commit-hash
    npm install github:expressjs/express#5.0

10. file:, git:, http: (Local & Remote References)

  • Installs from a local path, Git repo, or direct URL.
  • Examples:
    npm install file:../my-local-package
    npm install git+https://github.com/user/repo.git
    npm install http://example.com/package.tgz

Summary Table of Symbols

SymbolMeaningExample
@Specifies version or scoped packagenpm install lodash@4.17.21
^Allows minor & patch updates^4.18.24.19.0 allowed
~Allows only patch updates~4.18.24.18.3 allowed
>, <, >=, <=Version constraints>16.0.0
``
-Version range16.0.0 - 17.0.0
latestLatest stable versionnpm install lodash@latest
*Any version (wildcard)lodash@*
#Git commit/branch/taggithub:user/repo#main
file:, git:, http:Local/Git/URL installfile:../my-package

When to Use Which?

Exact version (@1.2.3) → For production stability.
Caret (^1.2.3) → Default, allows minor updates.
Tilde (~1.2.3) → Only bug fixes, stricter.
⚠️ Wildcard (*) → Risky, avoid in production.


This covers all major symbols in npm/npx. 🚀