npm commands, package.json fields, scripts, versioning, and workspace management
Install all dependencies from package.json
npm install
npm i # shorthand
npm install --production # skip devDependenciesInstall a package and add to dependencies
npm install react
npm install -D typescript # devDependency
npm install -g nodemon # global installExecute a script defined in package.json
npm run dev
npm run build
npm start # shorthand for npm run start
npm test # shorthand for npm run testUpdate packages to the latest allowed version
npm update # update all
npm update react # update one package
npm outdated # list outdated packagesRemove a package and update package.json
npm uninstall lodash
npm uninstall -D eslint # remove devDependencyPublish a package to the npm registry
npm login
npm publish
npm publish --access public # for scoped packagesRequired fields that identify the package
"name": "my-app",
"version": "1.0.0",Custom commands runnable with npm run <name>
"scripts": {
"dev": "next dev",
"build": "next build",
"start": "next start",
"lint": "eslint ."
},Packages required at runtime
"dependencies": {
"react": "^18.2.0",
"next": "14.0.0"
},Packages only needed during development and build
"devDependencies": {
"typescript": "^5.0.0",
"eslint": "^8.0.0"
},Packages the consumer must provide (plugins, libraries)
"peerDependencies": {
"react": ">=17.0.0"
},Allow minor and patch updates (compatible changes)
"react": "^18.2.0" // allows 18.x.x but not 19.x.xAllow only patch updates
"react": "~18.2.0" // allows 18.2.x but not 18.3.xLock to an exact version
"react": "18.2.0" // exactly this versionAccept a specific version range
"react": ">=17.0.0 <19.0.0"Execute a package binary without installing it globally
npx create-next-app@latest my-app
npx tsc --noEmit
npx prettier --write .Define workspaces in root package.json for monorepos
"workspaces": ["packages/*", "apps/*"]Run a script in a specific workspace
npm run build --workspace=packages/ui
npm run dev -w apps/webCommon scripts configuration for a Next.js project
{
"scripts": {
"dev": "next dev",
"build": "next build",
"start": "next start",
"lint": "next lint",
"lint:fix": "eslint . --fix",
"type-check": "tsc --noEmit",
"format": "prettier --write .",
"test": "jest",
"test:watch": "jest --watch",
"clean": "rm -rf .next node_modules"
}
}Common .npmrc settings for a project
# .npmrc
save-exact=true # use exact versions (no ^ or ~)
engine-strict=true # fail if Node version doesn't match
legacy-peer-deps=false # enforce peer dep resolution
@myorg:registry=https://npm.pkg.github.com # scoped registryWhy package-lock.json matters
# package-lock.json
# - Records the exact resolved version of every dependency
# - Must be committed to version control
# - npm ci installs from lockfile exactly (faster in CI)
npm ci # clean install from lockfile (CI/CD)
npm install # install + may update lockfile
# Never delete package-lock.json without a reason
# It ensures reproducible builds across environmentsAlways commit package-lock.json to ensure reproducible installs
Use npm ci in CI/CD pipelines instead of npm install for faster, deterministic builds
Prefer exact versions (save-exact=true in .npmrc) for critical dependency stability
Use npx for one-off tools to avoid polluting the global npm namespace
Audit your dependencies regularly with npm audit