Skip to content

How to ignore node_modules in Git?

  • by
  • 2 min read

Git is an important skill for just about every developer, let alone developers that work collaboratively in a team. However, while it can be easy to learn and commit your entire project directory to a repository, it’s sometimes better to leave out certain folders.

In this article, we’re taking a look at how you can ignore the node_modules folder in your Git commits. 


What is the node_modules folder and why shouldn’t it be committed?

If your project uses NPM or Yarn as the package manager, you’ll see a node_modules folder in the root folder of your project. This folder contains all the installed packages locally and keeps track of them in a file called packages.json. The folder also doubles up as a cache, meaning reinstalling packages doesn’t require an internet download. 

You shouldn’t be committing this folder to your online repository for a number of reasons:

  • The folder size can be in gigabytes.
  • It can easily be recreated using the packages.json file. 
  • Ideally, it’s redundant to commit code you didn’t originally write. 

Also read: How to fix Gitignore not working issue?


Ignoring node_modules in Git commits

You can ignore the node_modules folder and any other file or folder in your project directory by using the .gitignore fileOnce the file has been created, you can simply add the names of the files and folders to the file to tell Git to ignore them when making future commits. 

Keep in mind that if you’ve already committed the node_modules folder to your Git repository, the gitignore file will be ineffective as Git only ignores untracked files. To fix this, run the following command to remove the node_modules folder from the Git index.

git rm -r --cached node_modules

Once done, you can use the commit and push origin master commands with Git to push any tracked changes to the repository without any hassles. 

Also read: How to fix ‘Error:0308010c:digital envelope routines::unsupported’?

Yadullah Abidi

Yadullah Abidi

Yadullah is a Computer Science graduate who writes/edits/shoots/codes all things cybersecurity, gaming, and tech hardware. When he's not, he streams himself racing virtual cars. He's been writing and reporting on tech and cybersecurity with websites like Candid.Technology and MakeUseOf since 2018. You can contact him here: yadullahabidi@pm.me.

>