Click any command to expand the explanation and examples.
π¦ Project Setup
npm init basics
package.json file.
npm init # interactive prompts npm init -y # accept all defaults npm init @scope # use an initializer package
-y is the fastest way to start a new project.
package.json key fields basics
package.json.
{
"name": "my-app",
"version": "1.0.0",
"main": "index.js",
"type": "module",
"scripts": {
"dev": "vite",
"build": "vite build",
"test": "vitest"
},
"dependencies": {},
"devDependencies": {},
"engines": { "node": ">=18" }
}
Set "type": "module" to use ES module import/export syntax.
Semver versioning basics
MAJOR.MINOR.PATCH.
1.2.3 β β βββ patch: bug fixes (backward compatible) β βββββ minor: new features (backward compatible) βββββββ major: breaking changesIn package.json
β^1.2.3β β allows 1.x.x (minor + patch updates) β~1.2.3β β allows 1.2.x (patch updates only) β1.2.3β β exact version only
^ (caret) is the default and usually what you want.
π₯ Installing Packages
npm install daily
package.json.
npm install # install everything npm i # shorthandCreates
node_modules/ and package-lock.json. Always commit the lock file.
npm install <package> daily
npm install express # production dependency npm install -D vitest # devDependency (--save-dev) npm install -g typescript # global install npm install express@4.18.2 # specific version npm install @scope/package # scoped packageUse
-D for tools only needed during development (linters, test runners, bundlers).
npm uninstall daily
npm uninstall express npm uninstall -D vitest # remove from devDependencies npm uninstall -g typescript # remove global packageAlso removes it from
package.json and package-lock.json.
npm ci useful
npm ciDeletes
node_modules/ first, then installs exact versions from package-lock.json. Faster and more reliable than npm install in automated environments.
π Updating & Inspecting
npm update useful
npm update # update all npm update express # update one packageThis respects the
^ or ~ ranges in your package.json.
npm outdated useful
npm outdatedShows current, wanted (within semver range), and latest versions. Red = needs update.
npm list useful
npm list # full dependency tree npm list --depth=0 # top-level only npm list -g --depth=0 # global packages npm list express # check specific package
npm info <package> useful
npm info express npm info express version # latest version only npm info express versions # all published versions
π Scripts & Running
npm run <script> daily
package.json.
npm run dev npm run build npm run test npm run lint -- --fix # pass args to the scriptBuilt-in shortcuts:
npm start, npm test, npm stop don't need run.
npx daily
npx create-react-app my-app npx create-next-app@latest npx tsc --init npx eslint . npx -p node@18 node -v # run with specific Node versionDownloads and runs the package temporarily. Great for one-off commands and scaffolding tools.
Pre & post scripts advanced
pre and post scripts.
{
"scripts": {
"prebuild": "rm -rf dist",
"build": "tsc",
"postbuild": "cp package.json dist/"
}
}
prebuild runs before build, postbuild runs after.
π Security & Cache
npm audit security
npm audit # show vulnerabilities npm audit fix # auto-fix what's possible npm audit fix --force # fix even with breaking changes npm audit --json # machine-readable outputRun this regularly and before deploying to production.
npm cache troubleshooting
npm cache clean --force # clear the cache npm cache verify # check cache integrityClearing the cache can fix weird install issues.
π€ Publishing
npm login / npm publish publishing
npm login # authenticate npm publish # publish public package npm publish --access public # scoped package as public npm version patch # bump 1.0.0 β 1.0.1 npm version minor # bump 1.0.0 β 1.1.0 npm version major # bump 1.0.0 β 2.0.0
npm version updates package.json and creates a git tag.
npm pack publishing
npm pack # creates .tgz file npm pack --dry-run # preview what would be includedUseful for testing what your package looks like before publishing.
.npmignore & files field publishing
# .npmignore (like .gitignore for npm) src/ tests/ *.test.jsTheOr use βfilesβ in package.json (allowlist approach)
{ βfilesβ: [βdistβ, βREADME.mdβ] }
files field is preferred β itβs an allowlist instead of a blocklist.
ποΈ Workspaces
npm workspaces setup monorepo
# Root package.json
{
"workspaces": ["packages/*"]
}
Install all workspace dependencies
npm install
Run a script in a specific workspace
npm run build -w packages/ui
Run a script in all workspaces
npm run build βworkspaces
Workspaces share a single node_modules at the root, saving disk space and ensuring consistency.
npm link advanced
# In the library you're developing npm linkChanges to the linked package are reflected immediately β no need to reinstall.In the project that uses it
npm link my-library
Remove the link
npm unlink my-library
Quick Reference Table
| What you want to do | Command |
|---|---|
| Start a new project | npm init -y |
| Install all dependencies | npm install |
| Add a package | npm install <pkg> |
| Add a dev dependency | npm install -D <pkg> |
| Remove a package | npm uninstall <pkg> |
| Run a script | npm run <script> |
| Run a one-off command | npx <command> |
| Check for updates | npm outdated |
| Update packages | npm update |
| Check vulnerabilities | npm audit |
| Fix vulnerabilities | npm audit fix |
| Clean install (CI) | npm ci |
| Bump version | npm version patch |
| Publish package | npm publish |
| Clear cache | npm cache clean --force |