| name | npm-scripts |
| description | npm, yarn, and pnpm package management, scripts, workspaces, and publishing. Use when user asks to "run npm script", "setup package.json", "publish package", "manage dependencies", "npm workspaces", "monorepo setup", "npm audit", "lock file", "npmrc config", "npm registry", "debug npm script", "npx", "cross-platform script", or package manager operations. |
npm Scripts and Package Management
Package.json Scripts
Common Script Patterns
{
"scripts": {
"dev": "vite",
"build": "vite build",
"start": "node dist/index.js",
"test": "vitest",
"test:watch": "vitest watch",
"test:coverage": "vitest --coverage",
"lint": "eslint src/",
"lint:fix": "eslint src/ --fix",
"format": "prettier --write .",
"format:check": "prettier --check .",
"typecheck": "tsc --noEmit",
"clean": "rm -rf dist node_modules/.cache",
"prepare": "husky",
"validate": "npm-run-all --parallel lint typecheck test:coverage"
}
}
Pre/Post Hooks and Lifecycle Scripts
npm runs pre<script> before and post<script> after any script automatically:
{
"scripts": {
"prebuild": "npm run clean",
"build": "tsc",
"postbuild": "npm run copy-assets",
"clean": "rm -rf dist",
"copy-assets": "cp -r assets dist/",
"prepare": "husky",
"prepublishOnly": "npm run build && npm test",
"postinstall": "patch-package"
}
}
Built-in lifecycle scripts:
prepare -- runs after npm install and before npm publish
prepublishOnly -- runs before npm publish only (not on install)
preinstall / postinstall -- before/after npm install
prepack / postpack -- before/after tarball is created
Cross-Platform Scripts
{
"devDependencies": {
"cross-env": "^7.0.3",
"shx": "^0.3.4",
"npm-run-all2": "^6.0.0"
},
"scripts": {
"build": "cross-env NODE_ENV=production webpack",
"clean": "shx rm -rf dist",
"dev": "run-p watch:css watch:js serve",
"ci": "run-s lint typecheck test build",
"lint:all": "run-p lint:js lint:css lint:html"
}
}
run-s -- sequential; run-p -- parallel
- Glob patterns:
run-p lint:* runs all scripts matching lint:
Environment Variables
{
"scripts": {
"build:staging": "cross-env NODE_ENV=staging API_URL=https://staging.api.com vite build",
"build:prod": "cross-env NODE_ENV=production vite build"
}
}
npm exposes package.json fields as npm_package_* (e.g., npm_package_name, npm_package_version).
npx and bunx
npx create-next-app@latest my-app
npx -p typescript tsc --version
npx vitest
bunx create-next-app@latest my-app
Dependency Management
npm install
npm install lodash
npm install -D typescript
npm install lodash@4.17.21
npm install user/repo
npm ci
npm outdated
npm update
npx npm-check-updates -u
npm dedupe
npm why lodash
npm ls lodash
npm audit
npm audit fix
npm audit --omit=dev
Overrides and Resolutions
Force a transitive dependency version:
{
"overrides": { "glob": "^10.0.0" }
}
Yarn: "resolutions": { "glob": "^10.0.0" }. pnpm: "pnpm": { "overrides": { "glob": "^10.0.0" } }.
Lock Files
npm ci
npm ci --ignore-scripts
pnpm install --frozen-lockfile
yarn install --immutable
Regenerate only when resolving deep conflicts or migrating package managers: delete node_modules and lock file, then npm install.
.npmrc Configuration
Project-level .npmrc (commit this):
save-exact=true
registry=https://registry.npmjs.org/
@mycompany:registry=https://npm.mycompany.com/
engine-strict=true
User-level ~/.npmrc (never commit):
//registry.npmjs.org/:_authToken=${NPM_TOKEN}
//npm.mycompany.com/:_authToken=${COMPANY_NPM_TOKEN}
Custom Registry Setup
Verdaccio (local/private)
npm install -g verdaccio && verdaccio
npm set registry http://localhost:4873/
npm publish --registry http://localhost:4873/
GitHub Packages
@yourorg:registry=https://npm.pkg.github.com/
//npm.pkg.github.com/:_authToken=${GITHUB_TOKEN}
{ "publishConfig": { "registry": "https://npm.pkg.github.com/" } }
Workspaces (Monorepo)
Setup
{
"name": "my-monorepo",
"private": true,
"workspaces": ["packages/*", "apps/*"]
}
Workspace Commands
npm install
npm run build -w packages/shared
npm run build -w @myorg/shared
npm run test --workspaces
npm run build -ws --if-present
npm install zod -w packages/shared
npm install @myorg/shared -w apps/web
Monorepo Root Scripts
{
"scripts": {
"build": "npm run build --workspaces --if-present",
"test": "npm run test --workspaces --if-present",
"lint": "npm run lint --workspaces --if-present",
"dev:web": "npm run dev -w apps/web",
"dev:api": "npm run dev -w apps/api"
}
}
npm hoists shared deps to root node_modules. Conflicts stay in the workspace's own node_modules. Use npm dedupe if duplication creeps in.
pnpm
pnpm install
pnpm add lodash
pnpm add -D typescript
pnpm dlx create-next-app
packages:
- 'packages/*'
- 'apps/*'
pnpm -F web run build
pnpm -r run build
pnpm -r --parallel run dev
pnpm -F web... run build
yarn
yarn add lodash
yarn add -D typescript
yarn dev
yarn dlx create-next-app
yarn workspace web build
yarn workspaces foreach -A run build
Publishing
Package Setup
{
"name": "@scope/package",
"version": "1.0.0",
"main": "dist/index.js",
"module": "dist/index.mjs",
"types": "dist/index.d.ts",
"exports": {
".": {
"import": "./dist/index.mjs",
"require": "./dist/index.js",
"types": "./dist/index.d.ts"
}
},
"files": ["dist", "README.md", "LICENSE"],
"publishConfig": { "access": "public" },
"scripts": { "prepublishOnly": "npm run build && npm test" }
}
The files field whitelists what goes in the tarball. Prefer files over .npmignore.
Publish and Version
npm publish
npm publish --access public
npm publish --dry-run
npm pack --dry-run
npm publish --provenance
npm version patch
npm version minor
npm version major
npm version prerelease --preid=beta
npm version auto-updates package.json, creates a git commit, and tags it. Hook into the flow:
{
"scripts": {
"preversion": "npm test",
"version": "npm run build && git add -A",
"postversion": "git push && git push --tags && npm publish"
}
}
Debugging Scripts
npm run build --verbose
npm run build --silent
npm run
node --inspect-brk dist/index.js
NODE_OPTIONS='--inspect' npm run dev
DEBUG=express:* npm run dev
NODE_DEBUG=module node dist/index.js
Security
npm audit
npm audit --audit-level=high
npm audit signatures
npm ci
npm publish --provenance
npx @socketsecurity/cli report
Best practices:
- Run
npm audit in CI pipelines
- Use
npm ci (not npm install) in CI for reproducible builds
- Set
save-exact=true in .npmrc for critical dependencies
- Use
npm publish --provenance for public packages
- Never store auth tokens in committed
.npmrc -- use environment variables
Quick Reference
npm init -y
npm cache clean --force
npm config list
npm exec -- eslint .
npm link
npm repo
npm docs lodash
npm view lodash versions