Probably you forgot require('dotenv').config() at the top of server.js. Add it before any file that reads process.env. In production, env vars may be set by the host directly, but for local dev, dotenv is needed.
Common Express Server Setup Mistakes (and How to Fix Them)
Why do env vars come back undefined in production?
The answer
More FAQs in Common Express Server Setup Mistakes (and How to Fix Them)
Body parser missing or after routes, no error handler, hardcoded port, CORS misconfigured, .env not loaded, routers not mounted, handlers that forget to respond, misuse of next, sync heavy work in handlers, and console.log in production.
Because express.json() was not added or was added after the routes that read req.body. Add app.use(express.json()) before any route that needs to parse JSON request bodies.
Routers need to be mounted in app.js with app.use('/path', router). Creating a router in routes/auth.js is not enough; you must attach it to the Express app.
Still have questions?
Browse all our FAQs or reach out to our support team
Want to upskill yourself?
Your learning library
Small steps. Big possibilities.
Find your next course. Build something great.