Your Node.js app works locally but throws 'MongoNetworkError: connection timed out' when deployed to production. MongoDB connection strings work in MongoDB Compass but fail in the app.
This is usually caused by IP whitelist restrictions on MongoDB Atlas. Your production server's IP isn't whitelisted, or you're behind a dynamic IP that changed.
Step-by-Step Guide
Log into MongoDB Atlas dashboard and go to Network Access
Check if your production server IP is whitelisted (or add 0.0.0.0/0 for testing)
Verify your connection string includes correct username/password and cluster URL
Check if your connection string has +srv (for DNS seedlist)
Add connection options: useNewUrlParser: true, useUnifiedTopology: true
For Heroku/Vercel: whitelist 0.0.0.0/0 or use their IP range
Test connection from production server using mongo shell or Compass
Found an issue with this solution?