How to Ignore Node_modules Folder Everywhere

Ashok Chapagai Feb 02, 2024
  1. Ignore node_modules Folder Present in Root Folder
  2. Ignore All node_modules Folder Present in the Whole Project
How to Ignore Node_modules Folder Everywhere

While working on projects, there might be some folders you do not want git to be tracking; these can be the .env file, node_modules folder, and such.

These folders are meant to be for the local machines only and not to be shared among others. That might be because the node_modules folder’s size might vary from a few megabytes to even up to multiple gigabytes.

While working, there might be many changes to the node_modules folder that we surely do not want to track. Thus, we can use various ways to ignore the folder.

Ignore node_modules Folder Present in Root Folder

Let’s take the following folder structure:

.
|
├── .gitignore
├── node_modules
└── src
    └── index.html

Here, we need to set up our project so that we don’t include the folder node_modules tracked by git, which can be done by creating a .gitignore file. The files/folders mentioned inside .gitignore will not be tracked by git. So to ignore node_modules, the content inside the .gitignore folder should be as follows:

node_modules

Ignore All node_modules Folder Present in the Whole Project

To demonstrate this, we take the following project with the following folder structure:

.
├── backend
│   ├── index.html
│   └── node_modules
├── frontend
│   ├── index.html
│   └── node_modules
└── .gitignore

There are two node_modules folders inside the frontend and backend folders and just a single .gitignore file in the project’s root. To ignore both of the node_modules folders, the content of the .gitignore folder must be:

**/node_modules

Here, the two consecutive asterisks ** and followed by a slash / match in all directories to match the node_modules folder in both frontend and backend folders. Thus, this will make Git ignore both node_modules folders.

Ashok Chapagai avatar Ashok Chapagai avatar

Ashok is an avid learner and senior software engineer with a keen interest in cyber security. He loves articulating his experience with words to wider audience.

LinkedIn GitHub

Related Article - Git Ignore