Add node_module folder to .gitignore file

Add node_module folder to .gitignore file

In this post we will learn how to add node_module folder to .gitignore file to exclude it from Git.

In nodejs project, node_module folder is where all the packages needed for the JavaScript are downloaded and installed. But this folder is excluded from remote git repository because the size of the folder is very large.

Excluding the node_module folder from the remote repository do not create any issues because all the packages required by the project are listed in the package.json file.

So, anyone can clone the remote repository and run the command npm install and it will download all the package and create the node_module folder locally.

What is a .gitignore file ?

A .gitignore file is a plain text file which let us write the list of files and folders that we don't want Git to track from our project. It is usually placed in the root directory of our project.

Add node_module to .gitignore file

To exclude the node_module folder from tracking in Git we just have to add the folder name in the .gitignore file.

In .gitignore file


Once the folder name is added in the .gitignore file, node_module folder and it's sub-folder will be ignored and excluded by Git.

We can add multiple files and folders from the project that we don't want to be tracked by Git.

However, if the node_module folder is already committed and tracked by Git in remote repository then first we have to remove it from the git cache.

Note: If you are committing the files for the first time in git, then you don't have to follow the steps below.

To remove git cache, run the command:

git rm -r --cached .

This command will remove all the locally cached tracked files from Git. And now we have to add all the file again using git add . and commit the changes and push it to remote repository.

This time git will exclude the node_module folder from being tracked and commit the changes to remote repository.

Here is the full commands:

git rm -r --cached .
git add .
git commit -m "node_modules folder added to gitignore"
git push origin master

That's it, this is the simple way to exclude the node_module folder with .gitignore file.

Related Topics:

How To Force Git Pull To Overwrite Local Files ?

Resolve- Refusing To Merge Unrelated Histories Git Error