Although the node_modules folder is helpful in your local project, it contains code that is not part of your project and can be easily replicated on any machine. Hence, it is not essential to include this folder in your remote repository.
Instead, you should use the package.json file, which holds the name and versions of the packages required for your project.
This short article will provide simple commands to exclude the node_modules folder from your git repository.
What is the .gitignore file?
The .gitignore is a simple text file containing a list of files and directories you wish to exclude from the git repository.
Any file or directory that matches a specific pattern specified in the .gitignore file is ignored from your project.
In our case, we need to exclude the node_modules directory from our repository. We can do this by adding it to the .gitignore file.
Git ignore node_modules
Start by navigating to the root of your repository. Next, verify that the .gitignore file is available in your repo.
If the file does not exist, run the command below to create it.
Once you have the file created, open it with your text editor and add the following line at the end of the file:
Save and close the file.
Adding the above line into your .gitignore file will prevent git from tracking the node_modules folder in the repo.
If the node_modules folder has already been committed to the repository, run the command below to remove it from the staging area.
Next, add the files back, this time excluding the node_modules folder.
Finally, commit your changes.
Closing
This short tutorial provides instructions on removing the node_modules from your git repository.