Skip to main content
Exclude node_modules folder with .gitignore file

Exclude node_modules folder with .gitignore file

in this tutorial, we’ll learn about .gitignore file and how to use it to exclude node_modules from Git tracking. We can ignore folders that are already committed as well.

What’s mode_modules folder

All of the packages needed for your JavaScript project are downloaded and placed in the node modules/ subdirectory.

Due to its size and the fact that you shouldn’t add code that you didn’t write to the repository, this folder is frequently excluded.

What’s .gitgnore file

A list of file or folder patterns that Git must not track from your project can be entered in a .gitignore file, which is a plain text file. It’s frequently used to exclude files created automatically from your project.

How To Exclude node_module

To ignore the node_modules/ folder, you simply need to write the folder name inside .gitignore file:

node_modules/

The node_modules/ folder will be ignored by Git at the time of push. This works even when you have multiple node_modules/ folders located inside other subfolders.

Ignore the folder that has already been committed to a Git repository

If the node modules/ folder has already been committed and tracked by Git, you can force Git to forget about the folder by deleting it from Git cache using the command below:

git rm -r --cached .

The command above will remove all tracked files from the Git cache,

Now, you need to add all files, also need to commit the changes and push them into your remote repository. Here’s the full git command:

git rm -r --cached .
git add .
git commit -m "Remove node_modules folder"
git push

Leave a Reply

Your email address will not be published. Required fields are marked *