500 Internal Server Error
Learn what 500 Internal Server Error means, common causes, and how to debug and fix server-side failures.
What Does This Error Mean?
The 500 Internal Server Error is a generic error message indicating an unexpected condition on the server. It means the server encountered an error it does not know how to handle, and no more specific error code (like 502 or 503) applies.
Common Causes
Uncaught exception or unhandled promise rejection
Database connection failure or query timeout
Memory exhaustion or infinite loop
Missing environment variables or configuration
Syntax error in application code that crashes the process
File permission issues preventing read/write access
How to Fix It
Check server logs
The 500 error hides details from the client. Check your server logs for the actual error message and stack trace.
# View recent logs
heroku logs --tail
journalctl -u myapp -n 100
pm2 logs
# Enable request logging in Express
app.use(morgan("combined"))Add global error handling
Catch all uncaught exceptions and unhandled rejections to return meaningful error responses instead of crashing.
// Express global error handler
app.use((err, req, res, next) => {
console.error(err.stack)
res.status(err.status || 500).json({
error: process.env.NODE_ENV === "production"
? "Internal Server Error"
: err.message
})
})
// Catch unhandled rejections
process.on("unhandledRejection", (err) => {
console.error("Unhandled rejection:", err)
})Validate environment variables
Ensure all required environment variables are set before the application starts.
// Startup validation
const required = ["DATABASE_URL", "JWT_SECRET", "API_KEY"]
for (const env of required) {
if (!process.env[env]) {
throw new Error(`Missing required env: ${env}`)
}
}Related Tools
Use these tools to debug and fix this error:
Related Guides
Deepen your understanding with these guides and tutorials:
Related Errors
Other common errors in this category:
401 Unauthorized Error
Learn what a 401 Unauthorized error means, common causes, and how to fix authentication failures in your web applications.
403 Forbidden Error
Learn what 403 Forbidden means, how it differs from 401, and how to fix access denied errors in your applications.
404 Not Found Error
Learn what 404 Not Found means, common causes, and how to fix broken links and missing resources on your website or API.
429 Too Many Requests Error
Learn what 429 Too Many Requests means, how rate limiting works, and how to handle or avoid hitting API rate limits.
Frequently Asked Questions
Why does 500 show a generic message?
Showing detailed error information to clients is a security risk. Production servers should log the full error server-side and return only a generic 500 message to the client.
What is the difference between 500, 502, and 503?
500 means the server itself had an unexpected error. 502 Bad Gateway means an upstream server returned an invalid response. 503 Service Unavailable means the server is temporarily overloaded or down for maintenance.