Installation
Installing npm & Node
node -v && npm -v # check installed versions npm install -g npm@latest # update npm itself nvm install --lts # install Node LTS via nvm nvm use 20 # switch to Node 20
Install Commands
npm installInstall all dependencies from package.json
npm install pkgAdd package as dependency
npm install -D pkgAdd package as devDependency
npm install -g pkgInstall package globally
npm install pkg@2.1.0Install specific version
npm ciClean install from lock file (CI/CD)
npm uninstall pkgRemove package
Package Management
Managing Packages
npm ls # list installed packages npm ls --depth=0 # top-level only npm outdated # check for newer versions npm update # update within semver range npm audit # check for vulnerabilities
Management Commands
npm lsList installed packages as tree
npm outdatedShow packages with newer versions
npm update [pkg]Update packages within semver range
npm auditAudit dependencies for vulnerabilities
npm audit fixAuto-fix vulnerable dependencies
npm pruneRemove extraneous packages
npm dedupeFlatten dependency tree to reduce duplication
Scripts
Running Scripts
npm run build # run "build" script npm test # shortcut for "test" script npm start # shortcut for "start" script npm run lint -- --fix # pass args to script npm run dev & # run in background
Script Lifecycle
npm test / npm tRun scripts.test
npm startRun scripts.start
npm run <name>Run any custom script
pre<name>Runs automatically before
post<name>Runs automatically after
npm runList all available scripts
package.json
Initialize & Fields
npm init # interactive setup npm init -y # accept all defaults npm pkg set name="my-app" # set a field npm pkg get version # read a field
Key Fields
namePackage name (lowercase, no spaces)
versionCurrent version (semver: major.minor.patch)
mainEntry point for CommonJS (require)
moduleEntry point for ES modules (bundlers)
type"module" for ESM, "commonjs" for CJS (default)
scriptsNamed commands (build, test, start, etc.)
dependenciesProduction dependencies
devDependenciesDevelopment-only dependencies
enginesRequired Node/npm version ranges
Versioning
Version Commands
npm version patch # 1.0.0 → 1.0.1 npm version minor # 1.0.1 → 1.1.0 npm version major # 1.1.0 → 2.0.0 npm version 3.2.1 # set explicit version npm version prerelease --preid=beta # 1.0.0-beta.0
Semver Ranges
^1.2.3Compatible: >=1.2.3 <2.0.0 (default)
~1.2.3Patch-level: >=1.2.3 <1.3.0
1.2.3Exact version only
>=1.0.0 <2.0.0Explicit range
*Any version
1.x / 1.2.xWildcard ranges
latestLatest published version tag
Publishing
Publish Workflow
npm login # authenticate to registry npm publish # publish public package npm publish --access public # scoped package as public npm unpublish pkg@1.0.0 # remove specific version npm deprecate pkg@"<2" "Use v2+" # deprecate old versions
Publish Reference
npm loginAuthenticate with npm registry
npm publishPublish package to registry
npm packCreate tarball without publishing
npm unpublishRemove published version (within 72h)
npm deprecateMark versions as deprecated
.npmignoreFiles to exclude from published package
files (package.json)Allowlist of files to include in package
Workspaces
Workspace Commands
npm init -w packages/core # create workspace npm install -w packages/core lodash # install in workspace npm run build --workspaces # run in all workspaces npm run test -w packages/api # run in specific workspace npm ls --workspaces # list workspace deps
Workspace Config
workspaces (package.json)Array of workspace globs: ["packages/*"]
-w / --workspaceTarget a specific workspace
--workspacesRun command across all workspaces
--include-workspace-rootInclude root package in workspace operations
npm install (root)Installs all workspace dependencies
HoistingShared deps hoisted to root node_modules
npx
Running with npx
npx create-react-app my-app # run without installing npx tsc --init # run local or remote bin npx -p typescript tsc file.ts # specify package explicitly npx --yes create-next-app # skip install prompt npx node@18 -e "console.log('hi')" # run with specific Node
npx Options
npx cmdRun cmd from local node_modules/.bin or remote
npx -p pkg cmdInstall pkg, then run cmd
npx --yes cmdAuto-confirm installation prompt
npx --no cmdRefuse installation — fail if not local
npx -c 'cmd'Run shell command with npx PATH
npx node@verRun specific Node.js version
Configuration
Config Commands
npm config list # show current config npm config set registry https://r.npmjs.com/ npm config set init-author-name "Name" npm config get prefix # global install path npm config delete key # remove a config value
Config Reference
.npmrc (project)Per-project config file
~/.npmrcPer-user config file
registryPackage registry URL
save-exacttrue to pin exact versions on install
engine-stricttrue to enforce engines field
fundfalse to suppress funding messages
auditfalse to skip audit on install
Common Patterns
One-Liners
npm ls --depth=0 --json | jq '.dependencies | keys[]' npm outdated --long # show type and homepage npm cache clean --force # clear npm cache npm explain pkg # why is pkg installed? npm exec -- envinfo --system # system info for bug reports
Recipes
Lock file onlynpm ci — clean install from package-lock.json
Check licensesnpx license-checker --summary
Find unused depsnpx depcheck
Bundle sizenpx bundlephobia-cli pkg — check package size
Upgrade allnpx npm-check-updates -u && npm install
Local registrynpx verdaccio — run private registry