joseverissimo
Published 23 Sep, 2018

Useful NPM commands

Here are some useful commands that you can use to increase productivity when working on your node.js project.

Installing packages

The most used command is the one that allows you to install packages, like so npm install my-package. But did you know that there are other ways to install packages?

Bash
# Install a package
npm install my-package
# or
npm i my-package

# Install a package globally
npm install --global my-package
# or
npm install -g my-package

# Install a package as a production dependency
npm install --save my-package
# or
npm install -S my-package

# Install a package as a development dependency
npm install --save-dev my-package
# or
npm install -D my-package

Listing all dependencies

When you're working on a new project and you're in the experimenting phase sometimes you can end up with a lot of dependencies without realising it. So, here is a way to view all of the installed dependencies:

Bash
# List all dependencies
npm list
# or 
npm ls

# List all dependencies on level one
npm list --depth 0

# List all dependencies installed globally
npm -g list --depth 0

# List all outdated dependencies
npm outdated
# And all globally outdated dependencies
npm -g outdated

Remove clutter

And last, you can use npm prune to remove all the packages that are not listed in your package.json file, this is really useful when you're testing out several different packages and you want a quick way to delete any packages that were not saved as dependencies.

There are a lot more commands you can use in NPM cli and you can find those in the following link https://docs.npmjs.com/cli