I am developing an Express.js application with PostgreSQL and Passport.js for authentication. When I try to start my server, it crashes or fails to start:
- Using
nodemon
, I get this error:[nodemon] app crashed - waiting for file changes before starting...
- Using
node server.js
, no error is displayed, but the server does not start, and nothing happens in the terminal.
I have verified that my PostgreSQL database is up and running, and the configuration seems correct. I suspect the issue may be related to middleware setup, database connection, or some silent error I can’t see.
Environment
- Node.js version: v22.11.0
- Express version: 4.21.1
- PostgreSQL version: 17
- Passport.js version: 0.7.0
- bcrypt version: 5.1.1
- Operating System: Windows 11
Code
server.js
Here is my main server file (server.js
):
if (process.env.NODE_ENV !== "production") {
require("dotenv").config();
}
const express = require("express");
const app = express();
const bcrypt = require("bcrypt");
const passport = require("passport");
const initializePassport = require("./passport-config");
const flash = require("express-flash");
const session = require("express-session");
const methodOverride = require("method-override");
const { Pool } = require("pg");
const path = require("path");
const pgSession = require("connect-pg-simple")(session);
// Database configuration
const pool = new Pool({
user: process.env.DB_USER,
host: process.env.DB_HOST,
database: process.env.DB_DATABASE,
password: process.env.DB_PASSWORD,
port: process.env.DB_PORT,
});
// Test database connection
pool.query("SELECT NOW()", (err, res) => {
if (err) {
console.error("Database connection failed:", err);
process.exit(1); // Exit on error
} else {
console.log("Database connected:", res.rows[0]);
}
});
// Initialize Passport.js
initializePassport(
passport,
async (email) => {
const result = await pool.query("SELECT * FROM public.users WHERE email = $1", [email]);
return result.rows[0];
},
async (id) => {
const result = await pool.query("SELECT * FROM public.users WHERE id = $1", [id]);
return result.rows[0];
}
);
// Middleware setup
app.use(express.static(path.join(__dirname, "public")));
app.use(express.urlencoded({ extended: false }));
app.use(flash());
app.use(session({
store: new pgSession({ pool: pool, tableName: "session" }),
secret: process.env.SESSION_SECRET,
resave: false,
saveUninitialized: false
}));
app.use(passport.initialize());
app.use(passport.session());
app.use(methodOverride("_method"));
// Routes
app.get("/index.html", (req, res) => {
res.sendFile(path.join(__dirname, "public", "index.html"));
});
// Server startup
app.listen(3000, () => {
console.log("Server running on http://localhost:3000");
});
passport-config.js
Here is my Passport.js
configuration file:
const LocalStrategy = require("passport-local").Strategy;
const bcrypt = require("bcrypt");
function initialize(passport, getUserByEmail, getUserById) {
const authenticateUsers = async (email, password, done) => {
const user = await getUserByEmail(email);
if (user == null) {
return done(null, false, { message: "No user with that email" });
}
try {
if (await bcrypt.compare(password, user.password)) {
return done(null, user);
} else {
return done(null, false, { message: "Password incorrect" });
}
} catch (e) {
return done(e);
}
};
passport.use(new LocalStrategy({ usernameField: "email" }, authenticateUsers));
passport.serializeUser((user, done) => done(null, user.id));
passport.deserializeUser(async (id, done) => {
const user = await getUserById(id);
return done(null, user);
});
}
module.exports = initialize;
What I’ve Tried
-
Confirmed environment variables in
.env
file:DB_USER=postgres DB_HOST=localhost DB_DATABASE=mydb DB_PASSWORD=my_password DB_PORT=5432 SESSION_SECRET=mysecret
-
Ensured PostgreSQL is running and accessible:
psql -h localhost -U postgres -d mydbv
-
Added a console.log inside app.listen to check if it runs:
app.listen(3000, () => { console.log("Server running on http://localhost:3000"); });
-
Checked for syntax errors and ensured
node_modules
is up to date. -
Verified
pgSession
with PostgreSQL setup.
What I Expect
When running the server:
- npm run devStart (or nodemon server.js) should start the server and show Server running on http://localhost:3000.
- node server.js should launch the server without issues.
What Actually Happens
-
With
nodemon
, the app crashes immediately with:[nodemon] app crashed – waiting for file changes before starting…
-
With
node server.js
, nothing happens. The terminal is silent, and the server does not start.
Question
- How can I debug why my server crashes with
nodemon
and fails silently withnode
? - Is there a silent error in my middleware or database connection setup?
6