Open In App

What is the purpose of the ‘node_modules’ folder ?

The node_modules folder is a directory in NodeJS projects that stores third-party libraries and dependencies. It’s essential for managing dependencies, which are packages or modules that a NodeJS project relies on.

When you install a package using npm or Yarn, these tools download the package along with its dependencies and save them in the node_modules folder. This folder ensures that all required dependencies are available for the project to run successfully.



Introduction to node_modules Folder:

npm, the default and widely adopted package manager in the NodeJS ecosystem serves as a pivotal tool for installing and administering external modules within a NodeJS project. It also boasts the capability to install various command-line interface (CLI) tools and execute project scripts efficiently. The tracking of installed modules within a project is facilitated through the package.json file, residing in the project’s directory. This file encompasses:



Purpose of node_modules:

Understanding NPM:

The npm (Node Package Manager) serves as a crucial tool for managing packages within JavaScript and Node.js environments. It operates via the command line, enabling developers to efficiently handle project dependencies, share packages, and execute various tasks pertinent to package management and project development.

Upon executing npm install, npm fetches the requested modules and installs them into the designated node_modules folder, effectively storing all downloaded packages there.

Key npm commands include:

This is how you can install the npm module in your folder directory.

npm install

Understanding package.json

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:

{
"name": "my-react-app",
"version": "0.1.0",
"private": true,
"dependencies": {
"@testing-library/jest-dom": "^5.17.0",
"@testing-library/react": "^13.4.0",
"@testing-library/user-event": "^13.5.0",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"web-vitals": "^2.1.4"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": [
"react-app",
"react-app/jest"
]
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
},
"devDependencies": {
"autoprefixer": "^10.4.17",
"postcss": "^8.4.35",
"tailwindcss": "^3.4.1"
}

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.

Steps to Setup node_modules Folder:

Step 1: To set up the folder let’s first create a directory in our Nodejs project by writing the following commands in our terminal:

mkdir node_project 
cd node_project

Step 2: After setting up our project folder, create a package.json file with the command.

npm init -y

Step 3: 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  i express mongoose

Step 4: If for any reason the node_modules folder is not seen, it can be set by running the following command:

npm install

In addition, by storing dependencies locally within the project directory, it ensures that the project is self-contained and can be easily shared or deployed without worrying about external dependencies.

Conclusion:

Overall, npm is a powerful tool that simplifies package management and streamlines the development process in JavaScript and NodeJS projects. It’s widely used by developers and plays a central role in the NodeJS ecosystem.


Article Tags :