npm Cheatsheet
npm is the package manager for Node.js. This cheatsheet covers installing, managing, and publishing packages.
npm is the default package manager for Node.js, used to install and manage JavaScript dependencies. This cheatsheet covers the commands you use most.
Getting Started
Initialize a project.
npm init # interactive setup
npm init -y # accept all defaults
Installing Packages
Add dependencies.
npm install express # add dependency
npm install -D vitest # dev dependency
npm install -g typescript # global install
npm install lodash@4.17.21 # specific version
npm install # install from package.json
Removing & Updating
Manage installed packages.
npm uninstall express # remove package
npm update # update packages
npm outdated # list outdated
npm ls # list installed
Running Scripts
Execute package.json scripts.
npm run dev # run "dev" script
npm start # shortcut for "start"
npm test # shortcut for "test"
npm run build # run "build" script
package.json Scripts
Define custom commands.
{
"scripts": {
"dev": "next dev",
"build": "next build",
"test": "vitest"
}
}
npx
Run packages without installing.
npx create-next-app my-app
npx prettier --write .
npx tsc --init
Versioning
Bump and publish.
npm version patch # 1.0.0 -> 1.0.1
npm version minor # 1.0.0 -> 1.1.0
npm version major # 1.0.0 -> 2.0.0
npm publish # publish to registry
Auditing
Check for vulnerabilities.
npm audit # report issues
npm audit fix # auto-fix where possible
npm makes managing JavaScript dependencies simple. Learn these commands, then explore alternatives like pnpm and yarn for faster installs.
For full documentation, see https://docs.npmjs.com/