In this article, we will understand the node_modules folder, its purpose and work by learning the topics listed below:
- Introduction to node_modules folder
- Understanding NPM
- Understanding package.json
- Version control with node_modules folder
- Purpose of node_modules folder
Introduction to node_modules Folder
The node_modules folder is a directory downloaded automatically while setting up our node project. This folder plays a crucial role in Node.js, it acts as a directory that will store all the dependencies and modules your project is dependent on. This also implements version control which manages the development environment for every specific project.
Before we know what the purpose of the ‘node_modules’ folder is, it’s important to understand other teams related to it. Let’s see them.
Understanding NPM (Node Package Manage)
Whenever we install any dependency or module from the command `npm install` the npm fetches the requested module and installs it into our node_modules folder, all the downloaded packages are stored in this folder.
Some of the basic NPM commands are:
- npm init: This command is used to initialize a new Node project, after running this command the node prompts us asking for information about our project like project name, version etc.
- npm install: This command is used to install all the required dependencies and packages.
- npm -v: This command is used to check the npm version present on the system
- npm i packgae_name: This command is used to install any package present on the npm website, e.g. – npm i bcryptjs, npm i express, etc.
The package.json is a crucial file in a Node.js project, it is a configuration file that contains a list of all the external dependencies that our project contains. Whenever our node project is shared it is shared without the node_modules and to install the required modules to run our project the package.json file plays an important role as now all the modules are downloaded with reference to this file.
Here’s how a sample package.json file looks like:
This file contains a variety of external dependencies installed such as express, dotenv, mongoose and others.
Version Control with node_modules Folder
In version control systems like Git, developers while sharing their code/work don’t include the node_modules folder due to its large size, as it contains a lot of files and folders along with dependencies. In this case, to install the required packages, the package.json file comes into use with the help of which anyone can install the required packages by running the command “npm install” command.
When we run the npm install command the NPM reads the dependencies section in the package.json file and installs all the required packages into our node_modules folder.
Here is how the node_modules folder and package.json look like:
Purpose of node_modules Folder
Since now we have a good understanding of the topics above, it becomes much easier for us to understand the purpose of the node_modules folder.
The node_modules folder is a vital component of a project as it contains all the packages and modules that are necessary for running our project.
Think node_modules folder as a place where all the external modules that are required to make our web app run are present. This folder plays a crucial role in maintaining and managing our project while also implementing the version. All the newly added packages and modules get stored in the node_modules.
To put our knowledge into practice let us set up the node_modules folder.
Setting Up node_modules Folder
1. Creating a project folder:
To set up the folder let’s first create a directory in our Nodejs project by writing the following commands in our terminal:
The following commands create a folder named “node_project” and navigate to it.
2. Creating a package.json file:
After setting up our project folder, create a package.json file with the command:
npm init -y
This command creates a default package.json file with default values, however, we can change certain parts of it like project name, author, version etc. by using the command:
This is what our package.json looks like now:
3. Adding dependencies:
Now let us add some of the dependencies on which our project depends. As mentioned in the above sections, the npm install package_name command can be used to install our desired package.
npm install express mongoose
package.json after installing dependencies:
The following command installs the express and mongoose packages from NPM and adds them to the dependencies section in our package.json file, along with this the node_modules folder also appears.
If for any reason the node_modules folder is not seen, it can be set by running the following command:
In conclusion to this article, we have read about node_modules, package.json and npm, how they are related to each other and how the node_modules folder works as a backbone to our Node.js projects.
Below are some of the articles listed related to Node.