Ignore node_modules Folder from Git Using .gitignore

The popular runtime environment Node.js for back-end web development makes it easier to create more complex and large-scale apps. However, the complexity of the codebase and dependency management become more difficult.

As working on a project, developers frequently run across the issue of the node_modules directory growing quickly. Because of its constant expansion, integrating this directory into version control repositories presents a considerable challenge. Developers utilize the .gitignore file as a crucial tool for resolving this issue. This file is essential for reducing versioning issues and facilitating a simpler, more effective project dependency management process.

In this article, we will look at various elements of managing Node.js dependencies, explore the usage of .gitignore, and discover best practices for maintaining a clean and functional Git repository. Let’s start with a short introduction to node_modules.

Introduction to node_modules Folder

In most Node.js applications, the node_modules folder is a common folder. It acts as the area where Node.js stores project dependencies by default. These dependencies are usually downloaded and kept in the node_modules folder when you install additional modules for your Node.js project via npm (Node Package Manager) or yarn.

The node_modules directory serves the following main purposes:

  1. Node.js projects require dependencies, which are external libraries or modules. The main function of node_modules is to hold these dependencies. The project package includes information on these dependencies in package.json.
  2. Every Node.js project has the option to have a node_modules directory of its own, which guarantees that dependencies are kept within the project. Node.js allows projects to specify the exact versions of their dependencies in the package.json file.
  3. Project dependencies may be quickly installed in the node_modules directory by developers from the package.json file by using commands like yarn install or npm install.

Introduction to .gitignore File

Git version control allows the intentional removal of specific files and directories from tracking, which may be accomplished by using a special file called “.gitignore”. Placed at the root of the repository, this file acts as a configuration file to define untracked items that Git should ignore.

When you have new files that you want to ignore, you must manually modify and commit the .gitignore file. The .gitignore file includes patterns that are matched against file names in your repository to decide whether or not they should be ignored. Notably, there is no explicit “git ignore” command.

Creating a .gitignore File to Ignore node_modules

Step 1: To create a .gitignore file, first open your terminal with the project root directory and run the following command:

echo ""> .gitignore
After creating .gitignore file

In this image, we can see that after using the command “echo “”> .gitignore” an empty .gitignore file is created inside the root directory

Step 2: Open the .gitignore file, and specify the file names that you wish to ignore. Every file name has to be typed on a separate line, for example:

Ignore Node_Modules

This image shows that we have added a file named “input2.txt” and a folder named “node_modules” inside the .gitignore file as an example.

Step 3: Initialize and commit git in your terminal:

git init
git add .
git commit -m "First Commit"

Step 4: Verify the git repository’s status. Every time you make changes and commit, git will disregard the file or files you put into the .gitignore file:

Image After Commit

After committing the codebase with the .gitignore file you will be able to see which files are disregarded based on the patterns set in the .gitignore file.

The “git status —ignored” command is usually used to provide information about files in your repository that have been ignored, as seen in the image above.

Note: A .gitignore file is usually stored in the repository’s root directory. The parent directory and the current working directory are other names for the root directory. All of the project’s files and other folders are located in the root folder.

Why We Should Not Commit node_modules in GitHub?

  1. Reduced complexity in pull requests: There will be many more files involved in the process of a pull request or merging if the dependencies are being changed. Tools start to lag or even choose not to display the entire difference.
  2. Size of repository: The repository size is minimized by removing the node_modules directory from version control. It will eventually be faster to work with because you won’t have to add hundreds of MB of dependencies to your repository.
  3. To maintain a spotless Git history: The package.json and package-lock.json file updates are stored when a new package is added. All you need to keep is the change in the package-lock.json file when you wish to upgrade the package version.
  4. Dependency-related merge conflicts: Working with branches may result in merge conflicts involving dependency code rather than just your code.

Removing node_modules from Version Control

Step 1: Create a file name .gitignore and add a pattern for the file or folder that you wish to ignore inside .gitignore file:


Step 2: To remove the node_module from git we can use the command:

git rm -r --cached node_modules

Step 3: Now we can commit and push the changes in the code:

git commit -m "remove node_modules"
git push origin main 


To sum up, understanding how to use .gitignore for the node_modules directory is a prerequisite to using version control effectively for Node.js applications. If not managed carefully, the node_modules directory, which contains project dependencies, can have significant effects on repository size, cloning times, and overall project performance.



Anurag Pandey
Anurag Pandey
Articles: 20