How to Ignore node_modules in Git

Keeping node_modules out of Git starts with adding its directory rule to the repository ignore file. A repository ignore file stores exclusions as plain text, one entry per line.

I’ll show how to add the rule and remove node_modules files that Git already tracks.

TL;DR

Add the directory rule to the repository’s ignore file. If Git already tracks those files, remove them from its index with the cached removal command.

node_modules/

What is .gitignore?

A repository ignore file stores exclusions as plain text, one entry per line. A global excludes file suits local editor preferences because it affects only your Git setup.

  • Commit the ignore rule so every clone gets the same rule.
  • Keep package.json and package-lock.json tracked for npm projects.

How to ignore node_modules in Git step by step

I ran the staging and ignore checks in a small Git repository and inspected the result for a nested dependency file.

Step 1: Add the ignore rule

Create or edit the ignore file at the project root and add this line. The final slash marks a directory, while the absence of a leading slash lets the rule match below that file at any depth.

Git reads one entry per line, treats a leading # as a comment, and includes inline text in the entry itself.

node_modules/

Step 2: Stage the project and inspect status

Stage ordinary project files and check status. For a path that should be ignored, ask Git which rule matches it with the verbose check-ignore option.

git add .
git status --short
git check-ignore -v node_modules/demo/index.js

The example run printed this status and rule match:

A  .gitignore
A  app.js

Ignored dependency check:
.gitignore:1:node_modules/    node_modules/demo/index.js

Git’s short status uses two columns. The first column reports index changes, and the second reports working-tree changes. An A in the first column marks a staged addition, while ignored dependency files stay out of this ordinary listing.

Terminal showing Git ignoring node_modules/demo/index.js

The rule match confirms that this nested, untracked file is ignored. Status lists the ordinary project files that are staged, while the dependency file does not appear there.

Step 3: Remove previously tracked dependencies

If the directory was committed before you added the rule, use the recursive command to stage its tracked paths for removal.

cd gitignore-example && git rm -r --cached node_modules
git add .gitignore
git status --short

The command prints the staged deletion:

rm 'node_modules/demo/index.js'
D  node_modules/demo/index.js
Terminal showing node_modules removed from the Git index but kept locally

The D in the first status column means Git staged the tracked path for deletion. The dependency file remains in the working directory. Review the short status output before committing, and name every tracked dependency location in a monorepo.

npm ci removes an existing node_modules directory and installs from the lockfile. It stops rather than updating an out-of-sync lockfile, so run it from the project root after checking the committed package files.

Teammates who pull this commit may lose previously tracked dependency files from their checkout, so they should install dependencies again before running the project. Earlier copies remain in repository history unless that history is rewritten separately with collaborator coordination.

Fix common node_modules ignore problems

Use these diagnostics to find the cause when the directory appears in an unexpected status result.

The no-index option makes Git evaluate the ignore entry for a path already in the index.

cd gitignore-example && git check-ignore -v --no-index node_modules/demo/index.js

I ran the check and got this matching source and path:

.gitignore:1:node_modules/    node_modules/demo/index.js
Terminal showing git check-ignore finding the node_modules rule

The result names the ignore file, its line and the matching text. Git’s command documentation defines exit status 1 as no path matching an ignore rule. Confirm the supplied path if that happens.

Git also evaluates ignore files in nested directories. A more specific entry can affect the result, so inspect the named file and line before changing other files.

SymptomLikely causeWhat to do
Tracked files remain listedThey are already in the indexRun the cached removal command shown above, then review status
A nested directory is not ignoredRule is anchored or in a different directoryUse the directory entry in the repository root and check with the verbose option
No match is printedPath is tracked, absent, or another rule takes precedenceUse the command above with the path to inspect tracked files too

Conclusion

For more context, see CodeForGeek’s guides to the purpose of node_modules and npm, along with Git’s ignore documentation.

FAQ

These answers cover directory scope and previously tracked dependencies.

Does node_modules/ ignore nested folders?

Yes. In a repository-root .gitignore, this slashless directory rule matches node_modules directories below that location, including nested project directories.

Why is Git still tracking node_modules?

Ignore rules do not remove files already in Git’s index. Run git rm -r --cached node_modules, review the staged changes, and commit them.

Should package-lock.json be ignored too?

For an npm project, keep package-lock.json in the repository so installs can use the recorded dependency tree. Ignore node_modules, not the lockfile.

Anurag Pandey
Anurag Pandey

Anurag Pandey writes Node.js and TypeScript guides focused on setup, files, JSON, npm, and debugging. He also covers React and TypeScript concepts.

Articles: 18