For those not familiar, npm is the Node Package Manager. While not the only package manager available, it is the one that ships with Node by default. Technically, npm is both a registry (list of packages) and a command line tool (used to npm install and npm uninstall those packages).
The command line tool has a ton of functionality, which I cover in my free introduction to npm course. Today, we’re just going to focus on two of those commands, npm install and npm uninstall.
Initializing an npm project
In the next two sections we’re going to talk about the multiple ways you can install and uninstall packages with npm’s command line tool. For some of the approaches (all except installing/uninstalling globally) you’ll need a package.json file in your project.
If you’re not familiar with package.json files, check out my free course! (plug plug plug) but the gist of it is that you’ll need to run an npm init in your project like so:
There are three ways you can install software using npm’s command line tool. They are:
1. Install a package globally, so that you can use it at any time from the command line:
npm install -g package-name
2. Install a package locally, saving it to your project’s package.json:
npm install --save package-name
3. Install package locally, marking it as a “dev dependency”
npm install --save-dev package-name
Installing something as a dev dependency means that it is necessary for building the app, but is not to be deployed to production. A good example of this would be a build tool like gulp or grunt, which you need to build your assets but you don’t need to install on your production servers.
In action, the difference between –save and –save-dev is that packages installed with the –save-dev flag will not be installed when a user runs:
npm install --production
However, it will still be installed on a regular install.
npm has a well designed API, so all of the uninstall commands map directly to their install counterparts! Here are the appropriate examples:
1. Uninstall a package globally, so that you can no longer use it from the command line:
npm uninstall -g package-name
2. Uninstall a package locally, removing it from your project’s package.json:
npm uninstall --save package-name
3. Uninstall package locally, removing it as a “dev dependency”
npm uninstall --save-dev package-name