npm Cheat Sheet

Each row shows the terminal command, the VS Code way, and a one-line meaning. Tap a command to copy it. Replace <pkg> with a real package name.

Setup & info

Terminal: Ctrl+`
Check the installed Node version
Run in terminal
Check the installed npm version
Run in terminal
Create a package.json with defaults
NPM Scripts view in Explorer
List the scripts this project defines

Installing packages

Run after opening the project
Install everything in package.json
Run in terminal
Add a package to dependencies
Run in terminal
Add a package to devDependencies
Run in terminal
Install a tool machine-wide (global)
Run in terminal
Remove a package
Used by CI / deploys
Clean install, exactly from the lockfile

Running scripts

NPM Scripts → ▶ dev
Start the local development server
NPM Scripts → ▶ build
Build the production site to dist/
NPM Scripts → ▶ preview
Preview the built site locally
NPM Scripts → ▶ start
Shortcut for the "start" script
NPM Scripts → ▶ test
Shortcut for the "test" script
Run in terminal
Pass options through to the tool

Versions & updates

Run in terminal
See which packages are behind
Run in terminal
Update within your safe (caret) ranges
Run in terminal
Move one package to its newest major
Run in terminal
Scan for known vulnerabilities
Run in terminal
Auto-fix vulnerabilities where safe

npx (run without installing)

Run in terminal
Scaffold a new project (one-off)
Run in terminal
Run this project's local Astro
Run in terminal
Run a local tool without a global install

Troubleshooting

Delete the folder + file in Explorer
Wipe the install (then reinstall)
Run in terminal
Rebuild node_modules cleanly
Run in terminal
Clear a corrupted download cache
Run in terminal
Show why a package is installed

Advanced (publish · workspaces · config)

Run in terminal
Sign in so you can publish packages
Run in terminal
Bump the version + create a git tag
Run in terminal
Rehearse a publish (uploads nothing)
Run in terminal
Publish the package to the registry
Run in terminal
Add a dependency to one workspace
Run in terminal
Run a script in every workspace
Run in terminal
Change an npm setting (.npmrc)

Stuck? Nine times out of ten the fix is: delete node_modules and package-lock.json, then run npm install again. And remember — your real project is just package.json + your code; the rest can always be rebuilt.

FAQ

Frequently Asked Questions

What are the npm commands a beginner uses every day?
npm install to set up a project, npm run dev to work on it, npm run build to produce the final site, and npm install <pkg> to add a package.
How do I start working on a project I just cloned?
Run npm install once to download its packages, then npm run dev to launch the local development server.
How do I reset npm when something is wrong?
Remove node_modules and package-lock.json, then run npm install. See the Fixing Common Problems chapter for more.