A Guide to Installing Dependencies using NPM (Node Package Manager) in Node.js – Node.js Tutorial 4

Posted by


In this tutorial, we will be discussing how to install dependencies using NPM (Node Package Manager) in Node.js. Dependencies are external modules or packages that your Node.js application needs in order to function properly. NPM is the default package manager for Node.js and it allows you to easily manage and install these dependencies.

Before we begin, make sure you have Node.js installed on your machine. You can check if Node.js is installed by opening your terminal and running the following command:

node -v

If Node.js is installed, it will display the version number. If not, you can download and install Node.js from the official website (https://nodejs.org/).

Now that you have Node.js installed, let’s move on to installing dependencies using NPM.

Step 1: Creating a new Node.js project
First, create a new directory for your Node.js project. You can do this by opening your terminal and running the following command:

mkdir my-nodejs-project

Next, navigate to the newly created directory by running:

cd my-nodejs-project

Now, initialize a new Node.js project by running:

npm init -y

This will create a package.json file in your project directory. The package.json file is where you can define the dependencies for your project.

Step 2: Installing dependencies
To install a dependency using NPM, you can run the following command in your terminal:

npm install <dependency>

Replace <dependency> with the name of the package you want to install. For example, if you want to install the popular Express framework, you can run:

npm install express

NPM will then download and install the Express package along with any other packages it depends on. You will see a node_modules directory created in your project directory, which contains all of the installed dependencies.

Step 3: Saving dependencies to package.json
When you install a dependency, NPM will automatically save it to your package.json file. This allows you to easily keep track of the dependencies your project requires.

If you want to save a dependency as a production dependency (i.e., a dependency required for your production environment), you can add the --save flag when installing the package:

npm install <dependency> --save

If you want to save a dependency as a development dependency (i.e., a dependency only required for development purposes), you can add the --save-dev flag:

npm install <dependency> --save-dev

Step 4: Updating dependencies
To update a dependency to the latest version, you can run the following command:

npm update <dependency>

This will update the specified dependency to the latest version available.

Step 5: Removing dependencies
If you no longer need a specific dependency, you can remove it from your project by running:

npm uninstall <dependency>

This will remove the specified dependency from your project’s node_modules directory as well as from your package.json file.

That’s it! You now know how to install, save, update, and remove dependencies using NPM in Node.js. Dependencies are an essential part of Node.js development, and NPM makes managing them a breeze.

0 0 votes
Article Rating
4 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
@codebreakthrough
2 months ago

⭐Check out Filestack – https://calcur.tech/filestack
Node.js YouTube Playlist – https://calcur.tech/nodejs

@AnirudhMathur-qq9ch
2 months ago

These tutorials are what makes a software engineer.

@parris3142
2 months ago

thanks !

@PunmasterSTP
2 months ago

Installing dependencies? More like “Incredibly great playlist this is gonna be!” Thanks so much for making this Caleb.