Exclude node_modules Folder from Git using .gitignore
Table of Contents
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:
BASHnode_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:
BASHgit rm -r --cached node_modules
From here, Git will remove the node_modules
folder from your Git repository. Now commit your changes.
BASHgit commit -m "Remove node_modules"
Then push your changes to the remote repository.
BASHgit 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!
- How to Install Node on Windows, macOS and Linux
- Getting Started with Svelte
- Getting Started with Electron
- How to Set Up Cron Jobs in Linux
- How to deploy a Deno app using Docker
- How to deploy a MySQL Server using Docker
- How to deploy a Node app using Docker
- Getting Started with Sass
- Learn how to use v-model with a custom Vue component
- Using Puppeteer and Jest for End-to-End Testing
- Creating a Twitter bot with Node.js
- Building a Real-Time Note-Taking App with Vue and Firebase