Exclude node_modules Folder from Git using .gitignore

Updated onbyAlan Morel
Exclude node_modules Folder from Git using .gitignore

When you work in Node, you often will have a node_modules folder. This is because this is the folder where all the modules you have installed are stored.

This folder is not needed in your Git repository because it is not part of the source code and can be fairly large.

So, rather than having a node_modules folder in your Git repository, you should instead commit your package.json and package-lock.json files so that the user who clones your project can install the modules they need themselves.

In this post, we'll learn how we can exclude the node_modules folder from Git so that it is not included in the repository.

Excluding the node_modules folder from Git

The best way to exclude a folder from Git is to add a .gitignore file to the root of the project.

This file will contain a list of files and folders that you want to exclude from Git.

Inside the .gitignore file, you should add this line:

BASH
node_modules

This will tell Git not to track the node_modules folder, and therefore, ignore it when you commit your changes.

Remove Tracked node_modules Folder

If you've already committed your node_modules folder, you will need to tell Git to untrack it.

To do this, run this command:

BASH
git rm -r --cached node_modules

From here, Git will remove the node_modules folder from your Git repository. Now commit your changes.

BASH
git commit -m "Remove node_modules"

Then push your changes to the remote repository.

BASH
git push

That's it! Now, when you clone your project, the node_modules folder will not be included in the repository.

Conclusion

In this post, we've learned how to ignore the node_modules folder from Git. We also learned how to untrack it if you've already committed it.

Hopefully, this has been helpful and thanks for reading!

To learn more about web development, founding a start-up, and bootstrapping a SaaS, follow me on X!
Copyright © 2017 - 2024 Sabe.io. All rights reserved. Made with ❤ in NY.