{

gitignore nodejs |how to ignore node_modules


gitignore nodejs project Example

This tutorial will show you how to add a.gitignore file to your Nodejs application and its contents. The.gitignore file contains entries for the names of files and folder paths that can ignore when committing source code to nodejs projects.

In nodejs applications, which files or folders are ignored?

Below are list of file or folder types

  • dependencies
  • log files
  • dotenv environment variables configuration
  • serverless folder
  • IDE related configuration
  • cache files
  • typescript and javascript related files

how to ignore node_module dependencies in gitignore files

  • node_modules dependencies:

The application has a lot of dependencies specified in package.json. On Running npm install command, It installs to node_modules in application folder.

These are not required for repository commits. Adding node_modules path like below in a git ignore file and

json packages also ignored as seen below

node_modules/
jspm_packages/
  • bower_components dependencies

if you are using bower package manager, It generates bower_components folder,

So you have to add below entry

bower_components/

for webpack dependencies, you have to add web_modules folder as seen below

web_modules/

log file ignore

There are different package managers in nodejs.

npm is the default package manager available which comes with node default installation.

It generates npm-debug.log file in root folder

Similarly, yarn generates yarn-debug.log and yarn-error.log, and Lerna generates Lerna-debug.log.

So, You have to add all these log files in git ignore file as seen below

npm-debug.log
yarn-error.log
yarn-debug.log
lerna-debug.log

dotenv environment variables file

In node applications, if you are storing secret keys in environment files, Dotenv files provide .env files which are not required to commit to the repository.

.env
.env.test
.custom-env

code editors vscode

vscode generates .vscode file and vscod-test file generated for vscode project import and working with extensions.

.vscode-test
.vscode

cache files

There are some package managers as well as tools that generates temporary cache file which can ignored as seen below

.yarn/cache
.yarn/unplugged
.yarn/install-state.gz
.eslintcache
.cache
.parcel-cache
.npm

Sample node application .gitignore file example

Here is a complete .gitignore file example


node_modules/
jspm_packages/
bower_components/

npm-debug.log
yarn-error.log
yarn-debug.log
lerna-debug.log
.serverless

.env
.env.test
.custom-env

# cache files
.yarn/cache
.yarn/unplugged
.yarn/install-state.gz
.eslintcache
.cache
.parcel-cache
.npm

# vs editor related files
.vscode-test
.vscode

Conclusion

Finally, gitignore is an important file which contents you should be familiar with.

THE BEST NEWSLETTER ANYWHERE
Join 6,000 subscribers and get a daily digest of full stack tutorials delivered to your inbox directly.No spam ever. Unsubscribe any time.

Similar Posts
Subscribe
You'll get a notification every time a post gets published here.