Git – Ignore node_modules Folder Everywhere

A quick guide to ignoring node_modules in Git using a .gitignore file and removing them from the repository with the git rm command seamlessly.

Problem Statement

The node_modules directory contains all your project's dependencies. Since these files can be large and are recreated using package.json, it's best practice to exclude them from version control.

Solution

To ignore the node_modules folder from being tracked by Git Open (or create) a .gitignore file in the root of your project. Add the following line
node_modules/
Save the file. If node_modules was already being tracked, remove it using
git rm -r --cached node_modules
Then commit the changes
git commit -m "Ignore node_modules folder"
Also Read: Node JS Use Cases: When, Why & How to Use Node.js?
Push to your repository
git push origin main

Conclusion

Using a .gitignore file helps maintain a clean repository and reduces unnecessary data. Excluding node_modules also ensures faster cloning and a smaller repo size.
Related

To verify Node.js installation, run node -v or node --version. This checks if Node.js is installed and working correctly. Problem Before you run any Node.js…

22 Jan, 2026

Problem Node.js is single-threaded, so it can’t use multiple cores. In clustered apps, microservices or when spawning repeatable child processes, you need a way for…

16 Jan, 2026

How to Get the Project Root Path in Node.js When working with large Node.js projects, you often need to find the root directory at runtime…

15 Oct, 2025