Npm cheatsheet

Few npm commands I found very useful during development.

CommandDescription
npm -vshow current npm version installed
npm initinizialize npm project into the current folder, creates package.json
npm --helpshow npm help manual page
npm listshow a tree of every package found in the current folder
npm list -gsame as above ^^, but search also in global packages
npm list -g --depth=0same as above ^^, but do not show every package’s dependencies
npm list [package name]show a tree of every instance found in the current folder of that specific package
npm installinstall all packages in package.json
npm install [package name]install a package as dependency*
npm install [package name] --saveinstall a package as dependency (same as above)
npm install [package name] --save-devinstall a package as dev dependency
npm install --save username/repo#branch-name-or-commit-or-taginstall package from GitHub repository
npm uninstall [package name]uninstall a package
npm updateupdate top level packages
npm update --depth [number of levels]update dependencies of dependencies packages
npm update [package name] -gupdate global package installation
npm docs [package name]show README, official website, of the given package
npm outdatedshow packages that should be updated

🧨 !important By default, in node@5 the --save flag is implicit.

Therefore running these two commands you will have the same result:

npm i lodash

# is the same as
npm i lodash --save

they add a new line in your package.json into the dependecies object:

{
  "name": "test-project",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "Giulia Chiola",
  "license": "ISC",
  "dependencies": {
    "lodash": "^4.17.21"
  }
}

Aliases

AliasCommand
npm inpm install
npm i [package name] -Dnpm install [package name] --save-dev
npm lsnpm list
npm up [package name]npm update [package name]
npm un [package name]npm uninstall [package name]

Config

Set initial values for npm projects:

npm config set init-author-name "Your name"
npm config set init-author-email "your@email.com"
npm config set init-license MIT

⚡️ Bonus tip

npm-check is a useful tool to check for outdated, incorrect, and unused dependencies

📚 More info