gitignore nodejs |how to ignore node_modules
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
- 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 thenpm install command
, It installs to node_modules in the application folder.
These are not required for repository commits. Adding the node_modules
path like below in a git ignore file and
json packages are also ignored as seen below
node_modules/
jspm_packages/
- bower_components dependencies
if you are using the bower package manager, It generates the bower_components
folder,
So you have to add the 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 the npm-debug.log
file in the 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 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
generates .vscode
file and vscode-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 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
Conclusion
Finally, gitignore is an important file whose contents you should be familiar with. it