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-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.
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.
.gitignore file, you should add this line:
This will tell Git not to track the
node_modules folder, and therefore, ignore it when you commit your changes.
If you've already committed your
node_modules folder, you will need to tell Git to untrack it.
To do this, run this command:
git rm -r --cached node_modules
From here, Git will remove the
node_modules folder from your Git repository. Now commit your changes.
git commit -m "Remove node_modules"
Then push your changes to the remote repository.
That's it! Now, when you clone your project, the
node_modules folder will not be included in the repository.
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!
Leave us a message!