← coderrocketfuel.com

Stop Git From Committing the node_modules Directory in a Node.js Application

When you create new commits and push them to a remote Git repository, how do you tell Git to ignore the node_modules directory? And to not have its contents pushed to GitHub?

The node_modules directory is usually way too large to store on GitHub and is installed after the repository is cloned via npm install.

You can do this using the .gitignore file.

In the root location of your application directory, create a new .gitignore file and add this line of code to it:

/node_modules

That will tell Git to ignore that entire directory when pushing updates to your remote Git repository on GitHub or GitLab.

The .gitignore file should also include things like .env files, any build files/directories, log files, test outputs, and more.

Thanks for reading and happy coding!