Setting up a Node.js project with a proper .gitignore file
Make sure to set up a proper .gitignore
before initializing your node project git repository. There are certain files that you do not want to check into git.
To set up a Node.js project with a proper .gitignore
file, follow these steps:
1. Initialize a New Node.js Project
Create a new directory for your project:
mkdir my-node-project cd my-node-project
Initialize the Node.js project with a
package.json
file:npm init -y
2. Install Dependencies (if needed)
You can install any Node.js packages as needed. For example:
npm install express
3. Create a Proper .gitignore
Create a .gitignore
file in the root of your project directory. Here’s a basic example for a Node.js project:
touch .gitignore
Contents of .gitignore
:
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
# Runtime data
pids
*.pid
*.seed
*.pid.lock
# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov
# Coverage directory used by tools like istanbul
coverage
# nyc test coverage
.nyc_output
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt
# Bower dependency directory (https://bower.io/)
bower_components
# node-waf configuration
.lock-wscript
# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release
# Dependency directories
node_modules/
jspm_packages/
# Typescript v1 declaration files
typings/
# Optional npm cache directory
.npm
# Optional eslint cache
.eslintcache
# Optional REPL history
.node_repl_history
# dotenv environment variables file
.env
.env.test
This .gitignore
ensures that you don't commit:
node_modules/
directory (since dependencies can be reinstalled viapackage.json
)Logs, environment files (e.g.,
.env
), and coverage reportsTemporary build files, test outputs, and other generated data
4. Initialize a Git Repository
Once you have your
.gitignore
file, initialize a Git repository:git init
5. Commit Your Changes
Add your project files to the repository:
git add .
Commit the changes:
git commit -m "Initial commit with Node.js setup"
Your Node.js project is now properly set up with a .gitignore
to avoid committing unnecessary files to your Git repository.
Subscribe to my newsletter
Read articles from David Gostin directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
David Gostin
David Gostin
Full-Stack Web Developer with over 25 years of professional experience. I have experience in database development using Oracle, MySQL, and PostgreSQL. I have extensive experience with API and SQL development using PHP and associated frameworks. I am skilled with git/github and CI/CD. I have a good understanding of performance optimization from the server and OS level up to the application and database level. I am skilled with Linux setup, configuration, networking and command line scripting. My frontend experience includes: HTML, CSS, Sass, JavaScript, jQuery, React, Bootstrap and Tailwind CSS. I also have experience with Amazon EC2, RDS and S3.