Before writing any routes, you need a properly initialized Node.js project with Express installed and a sensible folder structure. This lesson walks through setting up a new Express project from an empty folder.
What You Need Before Starting
Express is distributed as an npm package, so you need Node.js and npm installed first (Node 18 or later is recommended for modern Express 4/5 projects). Everything else, the project folder, package.json, and dependencies, is created through the command line.
A typical setup takes three commands: create the project folder, run npm init, and install express. From there you create an entry file and start building routes.
mkdir my-express-app
cd my-express-app
npm init -y
npm install express
npm init -y creates a package.json with default values, and npm install express adds Express as a dependency and creates node_modules.
app.js creates and configures the Express app (middleware, routes) but does not call listen().
server.js imports app and calls app.listen(), this is what you actually run.
routes/, controllers/, and middleware/ grow naturally as the project gets bigger.
.env holds secrets and environment-specific config, never commit it to version control.
Express Setup Cheatsheet
The commands and files you touch when starting any new Express project.
Task
Command / File
Notes
Init project
npm init -y
Creates package.json with defaults
Install Express
npm install express
Adds Express as a dependency
Dev auto-restart
npm install -D nodemon
Restarts server on file changes
Run script
"dev": "nodemon server.js"
Add to package.json scripts
ESM opt-in
"type": "module"
Enables import/export syntax
Env vars
npm install dotenv
Loads .env into process.env
CommonJS vs ES Modules
Express works with both module systems. CommonJS (require/module.exports) is the historical default for Node and is what most existing tutorials, and this course, use. ES Modules (import/export) are enabled by adding "type": "module" to package.json.
// CommonJS
const express = require('express');
module.exports = app;
// ES Modules (package.json has "type": "module")
import express from 'express';
export default app;
Pick one style per project and stay consistent, mixing require and import in the same file is not supported.
Adding nodemon for Development
Restarting node server.js manually after every change is slow. nodemon watches your files and restarts the server automatically whenever you save.
Running npm install express without first running npm init, leaving no package.json to track it.
Committing the node_modules folder instead of adding it to .gitignore.
Mixing require() and import syntax in a project that only supports one module system.
Hardcoding secrets directly in source files instead of using .env and dotenv.
Key Takeaways
A new Express project starts with npm init -y followed by npm install express.
Separate app.js (configuration) from server.js (starting the listener) for testability.
Use nodemon in development so the server restarts automatically on file changes.
Decide early between CommonJS and ES Modules, and stay consistent across the project.
Pro Tip
Add a .gitignore with node_modules and .env on day one, before your first commit, so secrets and generated dependency folders never end up in version control.
Your project is initialized and Express is installed. Next, build your first working Express server in the First Express Server lesson.