NPM Commands Cheatsheet

Node package manager cheatsheet
Tags : node nodejs node.js javascript js npm

Basic commands
# Update npm
$ npm install -g npm@latest

# Create package.json
$ npm init
$ npm init -y

# Install dependencies
$ npm install
$ npm i

# Install / Uninstall a package
$ npm install webpack
$ npm i webpack@4.43.0
$ npm uninstall webpack

# Clear npm cache
$ npm cache clean --force
Manage packages
# List installed packages
$ npm list
$ npm ls

# Search for a package
$ npm search <package_name>

# Check updates
$ npm outdated

# Update a package
$ npm update webpack
package.json Empty package.json file
{
  "name": "project_name",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC"
}
Npm scripts
# In package.json file add
# You can now execute the script called "start"
"scripts": {
  "start": "node index.js"
}
$ npm start

# Examples
"scripts": {
  "build": "rm -r dist/ && mksir dist/ webpack",
  "lint": "eslint ./src",
  "test": "mocha tests/index.js",
  "watch": "webpack --watch"
}
$ npm run build
$ npm run lint
$ npm run test
$ npm run watch