git
node_modules
gitignore
version control
software development

Git - Ignore node_modules folder everywhere

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

In Git, ignoring node_modules is usually the right default because dependencies are reproducible from package.json and lock files, while the installed tree is large, noisy, and platform-specific. The main trick is knowing whether you want to ignore node_modules in one repository or in every repository on your machine.

Ignore node_modules in a Repository

For a normal project-level ignore, add this line to the repository's .gitignore:

gitignore
node_modules/

That pattern already ignores directories named node_modules anywhere inside the repository. You do not need a more complicated pattern for typical Node projects.

If the folder already exists and is currently untracked, that is enough. New node_modules directories will stay out of git status.

Stop Tracking It If It Was Already Committed

.gitignore only affects untracked files. If node_modules was already committed in the past, you must remove it from the index while keeping the files on disk:

bash
git rm -r --cached node_modules
git commit -m "Stop tracking node_modules"

If there are multiple nested node_modules directories, you can remove them all from the index after adding the ignore rule:

bash
git rm -r --cached .
git add .
git commit -m "Rebuild index with updated ignore rules"

That command sequence is broader, so use it carefully, but it is sometimes the simplest way to reapply ignore rules across the whole repository.

Ignore node_modules Everywhere on Your Machine

If you want Git to ignore node_modules in all repositories on your computer, use a global ignore file.

First, create a personal ignore file:

bash
touch ~/.gitignore_global
echo 'node_modules/' >> ~/.gitignore_global

Then tell Git to use it:

bash
git config --global core.excludesFile ~/.gitignore_global

After that, every repository on your machine will respect the global rule unless it explicitly tracks a node_modules directory already.

This is useful if you frequently create scratch Node projects and do not want to repeat the same ignore entry each time.

Repository Ignore vs Global Ignore

A repository .gitignore is shared with the team and belongs in the project. A global ignore is personal and applies only to your machine.

Use the repository file when the folder should be ignored by everyone. Use the global file for editor backups, OS-specific junk, or habits that matter only to your local setup.

For node_modules, the repository-level rule is usually the better choice because it reflects a project convention rather than a personal preference.

Why node_modules Should Stay Out of Git

The directory can contain thousands of files, many of which are generated differently across operating systems and CPU architectures. Tracking it bloats clones, slows status checks, and produces diffs that add no real value.

The source of truth should be:

  • 'package.json'
  • 'package-lock.json, yarn.lock, or pnpm-lock.yaml'

Those files describe what should be installed. The install directory is a build artifact, not the canonical dependency record.

Common Pitfalls

The most common mistake is adding node_modules/ to .gitignore and expecting Git to forget files that are already tracked. Ignore rules do not untrack existing content.

Another issue is confusing project-level and global ignore files. A global rule helps only on your machine, so teammates will not benefit from it unless the repository also includes the ignore entry.

It is also common to overcomplicate the pattern. In most repositories, node_modules/ is enough. Extra wildcards are usually unnecessary unless the project has very unusual ignore behavior.

Summary

  • Add node_modules/ to .gitignore to ignore the folder within a repository.
  • Use git rm -r --cached node_modules if the folder was already committed.
  • Use core.excludesFile and a global ignore file to ignore it across all local repositories.
  • Prefer repository-level ignore rules when the whole team should follow the same convention.
  • Keep lock files in Git even though node_modules itself should stay out.

Course illustration
Course illustration

All Rights Reserved.