Step-by-Step Guide: Setting Up a Production-Ready Node.js and Express App
Are you ready to take your Node.js and Express development skills to the next level? This guide walks you through building a modern Express app from scratch, integrating essential tools like TypeScript, Jest, ESLint, and Nodemon for a smooth development experience. Whether you’re new to Express or a seasoned developer, this step-by-step tutorial is packed with tips to streamline your workflow.
Step 1: Initialize Your Node.js Project
Start in an empty folder by running:
npm init -y
Install TypeScript and related packages:
npm install typescript ts-node @types/node tsconfig-node20-base --save-dev
Generate a tsconfig.json
file and extend it with node20-base
for modern configurations tailored for Node.js 20.
Step 2: Set Up Jest for Testing
Install testing dependencies:
npm install jest ts-jest @types/jest --save-dev
Create a jest.config.mjs
file to configure Jest with TypeScript support. Add utility functions and test them in the src/tests
directory to ensure everything works.
Step 3: Configure ESLint and Prettier
Maintain clean and error-free code by installing ESLint and Prettier:
npm install eslint prettier eslint-plugin-prettier eslint-config-prettier --save-dev
Run npm init @eslint/config
to generate an ESLint configuration. Integrate Prettier for automatic code formatting and enforce rules using the new flat configuration format.
Step 4: Install and Configure Express
Add Express and middleware:
npm install express cors morgan
npm install @types/express @types/cors @types/morgan --save-dev
Create a basic server in src/server.ts
, with middleware for logging, request parsing, and handling CORS. Add a health check route (GET /health
) to verify the server is running.
Step 5: Automate Development with Nodemon
Install Nodemon for automatic server restarts:
npm install nodemon --save-dev
Create a nodemon.json
file to watch for changes in .ts
and .json
files. Add a script to your package.json
:
"scripts": {
"dev": "nodemon"
}
Run the server with npm run dev
and see your changes reflected instantly.
Bonus: Test Your API
Install the REST Client extension in VS Code to test your API endpoints directly in the editor. Create an api-test.http
file and send requests to verify your app’s functionality.
Conclusion
In this guide, we built a modern Node.js Express app with TypeScript, Jest, ESLint, Prettier, and Nodemon. Each tool plays a vital role in enhancing development efficiency, code quality, and maintainability.