How to Git Ignore Node Modules Folder Globally

Avatar

By squashlabs, Last Updated: October 26, 2023

How to Git Ignore Node Modules Folder Globally

To git ignore the node_modules folder everywhere in your Git repository, follow these steps:

Step 1: Create or open the .gitignore file

First, make sure you have a .gitignore file in the root directory of your Git repository. If the file doesn’t exist, create it. If it already exists, open it in a text editor.

Step 2: Add the node_modules folder to .gitignore

Inside the .gitignore file, add the following line:

node_modules/

This line tells Git to ignore the node_modules folder and any subfolders and files within it.

Step 3: Save and commit the .gitignore file

Save the .gitignore file and commit it to your Git repository. From now on, Git will ignore the node_modules folder and its contents.

Alternative Approach: Global Git Ignore

If you want to ignore the node_modules folder in all of your Git repositories, you can use the global Git ignore feature.

A better way to build and deploy Web Apps

  Cloud Dev Environments
  Test/QA enviroments
  Staging

One-click preview environments for each branch of code.

Step 1: Create or open the global gitignore file

First, make sure you have a global Git ignore file. You can check if it exists by running the following command:

git config --global core.excludesfile

If the command returns a path, it means the global Git ignore file already exists. If it returns nothing, you need to create the file. You can create it by running:

touch ~/.gitignore_global

Step 2: Add the node_modules folder to the global gitignore

Open the global Git ignore file in a text editor and add the following line:

node_modules/

This line tells Git to ignore the node_modules folder and any subfolders and files within it globally.

Step 3: Save the global gitignore file

Save the global Git ignore file.

Step 4: Configure Git to use the global gitignore file

To configure Git to use the global Git ignore file, run the following command:

git config --global core.excludesfile ~/.gitignore_global

This command sets the global Git ignore file path.

Best Practices

Here are some best practices to consider when dealing with the node_modules folder in Git:

– Always add the node_modules folder to your .gitignore file or global Git ignore file. This folder can contain a large number of files and can significantly increase the size of your Git repository.
– Instead of committing the node_modules folder, commit a package.json file with your project’s dependencies. This way, when someone clones your repository, they can install the dependencies using npm install or yarn install.
– If you work with multiple developers on a project, make sure everyone on the team is aware of the node_modules folder’s exclusion in Git. This prevents unnecessary conflicts and reduces the size of the repository when pulling changes.

More Articles from the Git Tutorial: From Basics to Advanced Concepts series: