npm Cheat Sheet

essential npm commands

Those of us who spend a lot of time in the Node.js environment owe a debt of gratitude to npm (Node Package Manager). The npm registry and CLI are indispensable components of our workflow, and greatly reduce the difficulty of spinning up, updating, and collaborating on our Node.js projects.

However, if you are new to npm (or use it infrequently), it can be difficult to keep its 50 CLI commands in mind. Thankfully, you won’t often need many of these commands. Here in our npm cheat sheet, we’ll look at the commands essential to any project.

npm install

 npm install <package name>

Package installation is at the heart of npm, so it’s fitting that we start here. Without additional arguments, npm install will install all modules listed as dependencies in your package.json file. You can also specify a local package to install by appending the package’s name or the name of a containing folder.

Remote packages can also be installed by a number of methods, outlined here.

To install a package globally rather than locally, append the -g or --global tag to the command.

npm uninstall

 npm uninstall <package name>

Though not quite as fundamental as its counterpart, npm uninstall remains an essential command. Use npm uninstall <package name> to completely remove a local package. To remove a global package, append the -g or --global tag.

npm update

 npm update <package name>

When you need to update your project or a particular package (as well as dependencies listed in the appropriate package.json), npm update makes it easy. You can specify a package by name, or update all packages in a specified location by omitting the name. Globally installed packages can be specified by adding the -g or --global tag. Npm update will also respect the caret and tilde dependencies as specified in package.json.

npm ls

 npm ls

Use the ls command to display a list of all installed packages and their dependencies in the form of a logical dependency tree. Use the -g or --global tag to return a list of global rather than local packages. You can also use tags to limit the results. For example, --link will return only linked dependencies, while --depth=0 will display only top-level packages.

To return additional package information, use la or ll in place of ls.

npm search

 npm search <search terms ...>

The final entry in our npm cheat sheet is a command we find ourselves using time and again. npm search searches package metadata for all files in the registry matching the search terms supplied. Tags like --exclude and --searchopts can be used to constrain results, and RegEx terms are indicated with a / at the beginning of a term.

There are still more search options that you’ll want to know, and you can learn more about them here.

 

This is by no means an exhaustive list, but our npm cheat sheet lays out the basic commands that will get you off and running with npm.

If you think we should have included another npm command in our list of essentials, let us know on Twitter.