Open In App

What is the purpose of the ‘node_modules’ folder ?

Last Updated : 13 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

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:

  • All requisite modules for the project alongside their installed versions.
  • Comprehensive metadata pertaining to the project, including authorship, licensing details, and more.
  • Defined scripts capable of automating various tasks within the project.

Purpose of node_modules:

  • Code Reusability: Node modules allow users to reuse existing code instead of reinventing the wheel. This can save time and effort by leveraging the work of others in the NodeJS community.
  • Modularity: Node modules promote a modular approach to software development. By breaking down code into smaller, manageable modules, users can build more maintainable and scalable applications.
  • Dependency Management: Node modules help manage dependencies within a project. Dependencies are specified in a project’s package.json file, and npm handles the installation and versioning of these dependencies.
  • Encapsulation: Node modules encapsulate functionality, making it easier to understand and reason about different parts of an application. This improves code organization and helps prevent namespace collisions.
  • Community Contribution: Node modules foster collaboration within the NodeJS community. Developers can share their modules with others, contribute to existing modules, and provide feedback to improve the ecosystem as a whole.

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:

  • npm init: Initiates a new Node project, guiding users through project-specific details like name and version.
  • npm install: Installs all required dependencies and packages for the project.
  • npm -v: Verifies the npm version installed on the system.
  • npm install package_name: Installs any package available on the npm website, such as npm install script or npm install express.

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.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads