ExpressJS - Elastic Beanstalk 502 Bad Gateway

  1. Add "scripts": { "start": "node app.js" } to your package.json. If your main js file is not app.js, don't forget to rename it! :)

  2. In Beanstalk console: Configuration->Software add new env var: port: 8081 (PORT: 8081) enter image description here

Important: even if you add env var as port you still have to use PORT (uppercase). That is what solved my problem. I tried to use process.env.port, but it did not work. Use process.env.PORT instead.

const port = process.env.PORT || 3000;
app.listen(port, () => {
  console.log("Server is listening on: ", port);
});

From the EB App Dashboard, go to Configuration -> Software Configuration -> add value "npm start", or "node 'yourmainfile'" to the Node Command.

Even though AWS shows "Command to start the Node.js application. If an empty string is specified, app.js is used, then server.js, then "npm start" in that order", it looks like it doesn't run unless you specify.


In my case the issue was my port, my express server was listening on port 80. I just needed to change it to 8081 because i think the EB reverse proxy (nginx/apache) tries to redirect traffic to 8081 by default.

I figured it out by checking the AWS EB access logs.


I had the same problem tonight. It turned out that the node app had not been started even though the portal claims that it will run npm start by default.

Here is what I did to fix it:

  1. Create a directory named .ebextensions in the root of my project
  2. Inside .ebextensions create a file named nodecommand.config
  3. Inside the nodecommand.config add the following yaml:
option_settings:
  - namespace: aws:elasticbeanstalk:container:nodejs
    option_name: NodeCommand
    value: "npm start"

The full instructions are available here: https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/create_deploy_nodejs_express.html