Node Package Manager Basics: Install, Update and Uninstall package(s)

A Beginner’s Guide to npm

NPM is a package manager for JavaScript. It comes with Node.js. To double-check, you can run the following command in terminal:

npm --version

Using NPM, you can easily install modules/packages. But before you start adding any Node packages to your application, you need to create a file called package.json. If you already know about this file. Feel free to skip the section below.

Package.json

It is a manifest file with metadata about the project like project name, author(s), dependencies (modules that a project requires i.e. express). To create this file, navigate to your project folder and run the following command:

npm init

This will ask you some questions about the project (project name, version, description, etc.). You can either type your answer or skip/use default values by pressing enter. Once you are all done, you will see the package.json file in your project folder. If you do not want to go through all these questions, you can pass a yes flag with the command and, it will use the default values and create this file for you.

npm init -y

Installing a package

You can easily install a package by saying npm i package-name. Now, when you install a package, two things happen. First, package.json is appended, with a new property called dependencies. It has the name and the version installed of the package. Second, a node_modules folder is generated, at the root of the project, and all the installed packages will be inside this folder.

In previous versions of npm, the install command was used with a save flag. Without this flag, the package will install inside the node_modules folder, but the dependencies in package.json would not append themselves. So, if you see somewhere npm i package-name --save that is perfectly fine it’s just you do not need this flag anymore.

Now, let’s install express using the command below:

npm i express

Install a specific version of a package

Sometimes you will need to install a specific version of a package and not necessarily the latest one. To do so, append the install command like this npm i package-name@version

To install the 4.4.3 version of express, use the command:

npm i express@4.4.3

Updating packages

As you build your application, chances are there will be newer versions of packages that you have installed. So, to update the minor & patch version (if you do not know what I mean, you can read about semantic versioning here) of the already installed package run the following command:

npm update

Uninstalling a package

You can remove a package like this npm un package-name. When you uninstall a package, it is removed from the node_modules folder. As well as the package.json file is updated, and the package info is removed from dependencies.

Let’s remove the express package that you installed earlier, using command:

npm un express

Important

The good thing about the package.json file is when sharing your project with your friend or pushing code to GitHub, you do not need to include the node_modules folder because it contains all the third-party libraries and, there is no need to share them. The size of this folder can vary, and you do not have to wait for all this data to upload. You can either put node_modules into the .gitignore file or delete this folder. A person using your code can open the project and install all these libraries by a simple command npm install. This command will look into the package.json file dependencies, and it will install all listed packages.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store