When working on a Node.js project, the node_modules
folder can grow extremely large — often with thousands of files. Including it in your Git repository is not recommended. Instead, it should be excluded using a .gitignore
file.
In this blog, we’ll walk you through the why and how of ignoring node_modules
in Git using .gitignore
.
🚫 Why You Should Ignore node_modules
The node_modules
directory contains all your project’s dependencies, downloaded via npm
or yarn
. It should be excluded from Git for several reasons:
- Size: The folder is large and can bloat your repository.
- Reproducibility: Dependencies should be restored using
package.json
andpackage-lock.json
(oryarn.lock
). - Best Practice: Ignoring
node_modules
is standard in nearly all professional Node.js projects.
✅ Step-by-Step: How to Add node_modules
to .gitignore
1. Open Your Project Directory
Open your project folder in a code editor like VS Code or via the terminal.
2. Open or Create a .gitignore
File
In the root directory of your project:
- If
.gitignore
exists, open it. - If not, create one:
touch .gitignore
3. Add node_modules
to the File
Inside .gitignore
, add the following line:
node_modules/
This tells Git to ignore the entire
node_modules
directory.
4. Remove node_modules
from Git Tracking (If Already Tracked)
If node_modules
was added to Git before updating .gitignore
, remove it from tracking:
git rm -r --cached node_modules
Then commit the changes:
git commit -m "Ignore node_modules folder"
5. Verify It’s Ignored
You can verify it’s being ignored by running:
git status
You should no longer see node_modules
in the list of tracked files.
🧠 Pro Tip: Use a Standard .gitignore
Template
GitHub maintains language-specific .gitignore
templates:
You can use this as a baseline for your project.
🏁 Conclusion
Adding node_modules
to .gitignore
is essential for maintaining a clean and efficient Git workflow in Node.js projects. It keeps your repository lean and focused on code — not bulky dependencies.