This post talks about how to add .gitignore file to nodejs applications and it its contents. .gitignore is a file that contains entries of name of files and folder path which are ignored in a project while committing source code to nodejs projects.
which files or folders are ignored in nodejs applications?
- log files
- dotenv environment variables configuration
- serverless folder
- IDE related configuration
- cache files
how to ignore node_module dependencies in gitignore files
node_modules dependeices: There are lot of dependencies configured in package.json, This will be installed to project in node_modules folder, These are not required to commit to repository.
Adding node_modules path like below in a git ignore file and
jspm packages also ignored as seen below
if you are using bower package manager, It generates bower_components folder,
So you have to add below entry
for showpack dependencies, you have to add web_modules folder as seen below
log file ignore
There are different package managers in nodejs,
npm is default package manager available which comes with node default installation
It generates npm-debug.log
Similarly, yarn generates yarn-debug.log and yarn-error.log, 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 provides .env files which are not required to commit to 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
There are some package managers as well as tools generates temparory cache file which can be 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
In conclusion, gitignore is very important file which you need to know what are contents of it.