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 is a list of file or folder types
- log files
- dotenv environment variables configuration
- serverless folder
- IDE-related configuration
- cache 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 the
npm install command, It installs to node_modules in the application folder.
These are not required for repository commits.
node_modules path like below in a git ignore file and
json packages are also ignored as seen below
- bower_components dependencies
if you are using the bower package manager, It generates the
So you have to add the below entry
for webpack dependencies, you have to add web_modules folder as seen below
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 the
npm-debug.log file in the root folder
Similarly, yarn generates
yarn-error.log, and Lerna generates
So, You have to add all these log files in the 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-test file generated for vscode project import and working with extensions.
There are some package managers as well as tools that generate temporary cache files 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
Finally, gitignore is an important file whose contents you should be familiar with. it