Why does my Express.js server crash when using nodemon, and fail silently with node server.js?

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:

  1. Using nodemon, I get this error:
    [nodemon] app crashed - waiting for file changes before starting...
    
  2. 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

  1. 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
    
  2. Ensured PostgreSQL is running and accessible:

    psql -h localhost -U postgres -d mydbv
    
  3. Added a console.log inside app.listen to check if it runs:

    app.listen(3000, () => {
            console.log("Server running on http://localhost:3000");
        });
    
  4. Checked for syntax errors and ensured node_modules is up to date.

  5. 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

  1. How can I debug why my server crashes with nodemon and fails silently with node?
  2. Is there a silent error in my middleware or database connection setup?

6

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa

Why does my Express.js server crash when using nodemon, and fail silently with node server.js?

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:

  1. Using nodemon, I get this error:
    [nodemon] app crashed - waiting for file changes before starting...
    
  2. 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

  1. 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
    
  2. Ensured PostgreSQL is running and accessible:

    psql -h localhost -U postgres -d mydbv
    
  3. Added a console.log inside app.listen to check if it runs:

    app.listen(3000, () => {
            console.log("Server running on http://localhost:3000");
        });
    
  4. Checked for syntax errors and ensured node_modules is up to date.

  5. 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

  1. How can I debug why my server crashes with nodemon and fails silently with node?
  2. Is there a silent error in my middleware or database connection setup?

6

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật