Cheatsheet npm / yarn
Gestores de pacotes para JavaScript e Node.js
npm / yarn
23
cards found
Categories:
Versions:
npm
Start a Project
npm init # interactive
npm init -y # accept defaults
npm init @scope/app # template
Create package.json.
Install Packages
npm install # everything
npm install lodash # dependency
npm i -D jest # devDependency
npm i -g typescript # global
npm i lodash@4.17.21 # fixed version
Add dependencies.
Remove and Update
npm uninstall lodash
npm update # update everything
npm outdated # see outdated
npm audit # vulnerabilities
npm audit fix # auto fix
Maintain dependencies.
Info and List
npm list # deps tree
npm list --depth=0 # direct only
npm info lodash # package info
npm view lodash versions
Explore dependencies.
Yarn
Basic Commands
yarn init # create project
yarn # install everything
yarn add lodash # dependency
yarn add -D jest # devDependency
yarn remove lodash # remove
Classic Yarn (v1).
Yarn Berry (v2+)
corepack enable
yarn set version stable
yarn install # PnP by default
yarn dlx create-react-app my-app
yarn workspaces foreach run build
Modern Yarn.
Info and Upgrade
yarn list # dependencies
yarn outdated # outdated
yarn upgrade # update
yarn info lodash # details
yarn why lodash # who depends on it
Manage packages.
Cache and Cleanup
yarn cache dir # see cache
yarn cache clean # clear cache
yarn install --force # reinstall
node_modules/.yarn-state.yml
Yarn cache.
Scripts
Define Scripts
// package.json
"scripts": {
"dev": "vite",
"build": "vite build",
"test": "jest --coverage",
"lint": "eslint src/"
}
Scripts in package.json.
Run Scripts
npm run dev
npm run build
npm test # shortcut (no run)
npm start # shortcut
yarn dev # yarn (no run)
Run scripts.
Pre/Post Hooks
"scripts": {
"prebuild": "npm run clean",
"build": "webpack",
"postbuild": "npm run copy-assets"
}
// pre/post run automaticallyLifecycle hooks.
Pass Arguments
npm run test -- --watch
npm run build -- --mode production
yarn test --watch # yarn does not need --
// In the script: use process.argv
Arguments for scripts.
Dependencies
Versions (semver)
"lodash": "4.17.21" # exact
"lodash": "^4.17.0" # compat (minor)
"lodash": "~4.17.0" # patch only
"lodash": "*" # any
"lodash": ">=4.0 <5.0"
Semantic versioning.
package-lock vs yarn.lock
package-lock.json (npm)
yarn.lock (yarn)
# ALWAYS commit them!
# Guarantees reproducible builds
# npm ci → installs exactly from lock
Lock files.
npm ci vs npm install
npm ci # clean, from lock (CI)
npm install # may update lock
# npm ci:
# • Removes node_modules
# • Installs exactly from lock
# • Faster in CI
Important differences.
Optional Dependencies
"optionalDependencies": {
"fsevents": "^2.3.0"
}
"peerDependencies": {
"react": ">=17.0.0"
}Special dependency types.
Publication
Publish a Package
npm login
npm publish
npm publish --access public
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
Publish to the npm registry.
Prepare a Package
// package.json
"name": "@scope/package",
"version": "1.0.0",
"main": "dist/index.js",
"files": ["dist/"],
"prepublishOnly": "npm run build"
Configure for publishing.
Private Registry
npm publish --registry http://local:4873
// .npmrc
registry=https://registry.npmjs.org/
@scope:registry=http://private:4873
//token=${NPM_TOKEN}
Use a private registry.
Advanced
npx
npx create-react-app my-app
npx prettier --write .
npx http-server dist/
npx cowsay "Hello"
# Runs without installing globally
Run temporary packages.
Workspaces (monorepo)
// root package.json
"workspaces": ["packages/*"]
npm run build --workspace=pkg-a
yarn workspaces foreach run build
yarn workspace pkg-a add lodash
Monorepo with workspaces.
Security
npm audit # check vulns
npm audit fix # auto fix
npm audit fix --force # breaking
yarn audit # yarn
npm config set audit-level high
Security auditing.
.npmrc and Config
npm config list
npm config set save-exact true
npm config set prefix ~/.npm-global
// project .npmrc:
save-exact=true
engine-strict=true
npm configuration.