Error Encyclopedia
500

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

1

Uncaught exception or unhandled promise rejection

2

Database connection failure or query timeout

3

Memory exhaustion or infinite loop

4

Missing environment variables or configuration

5

Syntax error in application code that crashes the process

6

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:

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.