Introduction

Node.js has become the backbone of modern backend development, powering everything from REST APIs to real-time chat applications. But setting up a project correctly from the start — with proper folder structure, package management, environment configuration, and linting — can save hours of refactoring down the line.

This guide walks you through setting up a clean, production-ready Node.js project from scratch.

Prerequisites

  • Node.js installed (LTS version recommended — download from nodejs.org)
  • Basic familiarity with the command line
  • A code editor (VS Code recommended)

Step 1: Initialize the Project

Open your terminal, navigate to your desired directory, and run:

mkdir my-node-app
cd my-node-app
npm init -y

This creates a package.json file with default settings. The -y flag skips the interactive prompts — you can edit the file manually afterward.

Step 2: Set Up Your Folder Structure

A clean folder structure makes the codebase easier to navigate and scale:

my-node-app/
├── src/
│   ├── routes/
│   ├── controllers/
│   ├── models/
│   └── index.js
├── .env
├── .gitignore
├── package.json
└── README.md

Step 3: Install Core Dependencies

For a typical Express-based API project, install the following:

npm install express dotenv
npm install --save-dev nodemon eslint
  • express: Minimal web framework for handling routes and middleware.
  • dotenv: Loads environment variables from a .env file.
  • nodemon: Automatically restarts the server when file changes are detected (dev only).
  • eslint: Catches code quality issues early.

Step 4: Configure Environment Variables

Create a .env file in your root directory:

PORT=3000
NODE_ENV=development

Then add .env to your .gitignore file to prevent sensitive credentials from being committed to version control. This is a critical security practice.

Step 5: Create Your Entry Point

In src/index.js, set up a basic Express server:

require('dotenv').config();
const express = require('express');
const app = express();
const PORT = process.env.PORT || 3000;

app.use(express.json());

app.get('/', (req, res) => {
  res.json({ message: 'Server is running!' });
});

app.listen(PORT, () => {
  console.log(`Server listening on port ${PORT}`);
});

Step 6: Add NPM Scripts

Update your package.json scripts section:

"scripts": {
  "start": "node src/index.js",
  "dev": "nodemon src/index.js",
  "lint": "eslint src/"
}

Now you can run npm run dev for development with hot-reloading.

Step 7: Initialize Git

  1. Run git init in the project root.
  2. Create a .gitignore file that includes node_modules/ and .env.
  3. Make your first commit: git add . && git commit -m "Initial project setup"

You're Ready to Build

With this foundation in place, you can add database connections (MongoDB, PostgreSQL), authentication middleware, and route handlers systematically. Starting with a clean structure makes every subsequent addition simpler and your codebase easier to hand off to teammates.