Skip to content

Instantly share code, notes, and snippets.

@Dom-Mc
Last active August 10, 2017 01:27
Show Gist options
  • Save Dom-Mc/b76bc52b61fb5a36500763bf1e16ace4 to your computer and use it in GitHub Desktop.
Save Dom-Mc/b76bc52b61fb5a36500763bf1e16ace4 to your computer and use it in GitHub Desktop.
npm commands

npm

Help

  • List of commands npm or npm help
  • Display full usage info of all commands npm -l
  • Display usage info of specific command $ npm <command> -h

Scripts

  • Write scripts to run in package.json file
 "scripts": {
   "lint": "./node_modules/.bin/eslint"
 }

List packages

  • npm list -g
  • npm ls -g --depth=0

Remove the unwanted dependencies by calling

  • When you run prune, the npm CLI will run through your package.json and compare it to your project’s /node_modules directory. It will print a list of modules that aren’t in your package.json.
  • npm prune

Begin a project

  • Initializes project as a node project
  • Will ask you a bunch of questions, and then write a package.json for you.
  • npm init

Installing packages

  • npm install <package name>
  • npm i <package name>
  • npm init --yes will install defaults

Installing as a dependency

  • The --save option instructs NPM to include the package inside of the dependencies section of your package.json
  • npm install --save
  • npm install -S
  • npm i -S

Installing as a Dev Dependency (devDependencies)

  • npm install --save-dev
  • npm install -D
  • npm i -D

Installing a specific package version

  • npm install <package name@<version number>
  • Example: npm install underscore@1.8.2

Update local packages

  • npm update
  • npm update <package name>

Check for outdated packages

  • npm outdated
  • npm outdated -g

Uninstall packages (r shortcut)

  • npm uninstall <package name>
  • npm r <package name> --save
  • npm r <package name> --save-dev

Uninstall local dependency & remove from package.json

  • npm uninstall <package name> --save-dev

Uninstall global package

  • npm uninstall <package name> -g

Provides info regarding local install

  • npm config list

Find the path to npm's directory

  • npm config get prefix

Cache

  • When npm installs a package it keeps a copy, so the next time you want to install that package, it doesn’t need to hit the network. The copies are cached in the .npm directory in your home path.
  • This directory will get cluttered with old packages over time, so it’s useful to clean it up occasionally.
  • npm cache clean

Run Tests

  • npm test
  • npm t
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment