Exclude node_modules folder with .gitignore file

The node_modules/ folder is the folder where all packages required by your JavaScript project are downloaded and installed. This folder is commonly excluded from a remote repository because it has a large size and you shouldn’t add code that you didn’t write to the repository.

Rather than including the node_modules/ folder, you should have the packages required by your project listed in package.json file and ignore the node_modules/ folder using the .gitignore file.

A .gitignore file is a plain text file where you can write a list of patterns for files or folders that Git must not track from your project. It’s commonly used to exclude auto-generated files in your project.

To ignore the node_modules/ folder, you simply need to write the folder name inside .gitignore file:

node_modules/

And with that, your node_modules/ folder will be ignored by Git. This works even when you have multiple node_modules/ folders located inside another subfolders.

If you already have the node_modules/ folder committed and tracked by Git, you can make Git forget about the folder by removing it from Git cache with the following command:

git rm -r --cached .

The command above will remove all tracked files from Git cache, so you need to add them back using git add . where the dot (.) symbol will add all files in the folder, but still exclude those in the .gitignore file.

Next, you just need to commit the changes and push them into your remote repository. Here’s the full git command:

git rm -r --cached .
git add .
git commit -m "Remove node_modules folder"
git push

And that’s how you ignore the node_modules/ folder using .gitignore file. Feel free to modify the commands above as you require 😉

Take your skills to the next level ⚡️

I'm sending out an occasional email with the latest tutorials on programming, web development, and statistics. Drop your email in the box below and I'll send new stuff straight into your inbox!

No spam. Unsubscribe anytime.