Git

gitignore node_modules

The node_modules folder is a folder that contains all the packages installed and required by your JavaScript project.

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.

$ touch .gitignore

Once you have the file created, open it with your text editor and add the following line at the end of the file:

node_modules/

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.

$ git rm -r --cached

Next, add the files back, this time excluding the node_modules folder.

$ git add .

Finally, commit your changes.

$ git commit -m 'Exclude node_modules directory'

Closing

This short tutorial provides instructions on removing the node_modules from your git repository.

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list