title: Nodejs/express api fails at 502 bad gateway on elastic beanstalk
body: <p>I've attempted to launch a nodejs/express api on Elastic Beanstalk.</p>
  
  <p>When I run the api locally, it works perfectly.</p>
  
  <p>When I go to the URL given by elastic beanstalk, I reach a 502 bad gateway.</p>
  
  <p>This is the error log that I receive:</p>
  
  <pre><code>Error: Cannot find module '/var/app/current/main.js'
      at Function.Module._resolveFilename (internal/modules/cjs/loader.js:636:15)
      at Function.Module._load (internal/modules/cjs/loader.js:562:25)
      at Function.Module.runMain (internal/modules/cjs/loader.js:831:12)
      at startup (internal/bootstrap/node.js:283:19)
      at bootstrapNodeJSCore (internal/bootstrap/node.js:622:3)
  npm ERR! code ELIFECYCLE
  npm ERR! errno 1
  npm ERR! backend@1.0.0 start: `node main.js`
  npm ERR! Exit status 1
  npm ERR! 
  npm ERR! Failed at the backend@1.0.0 start script.
  npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
  
  &gt; backend@1.0.0 start /var/app/current
  &gt; node main.js
  
  internal/modules/cjs/loader.js:638
      throw err;
  </code></pre>
  
  <p>This is my main.js:</p>
  
  <pre><code>
  const express = require('express')
  const app = express()
  const bodyParser = require('body-parser')
  const cors = require('cors')
  const mongoose = require('mongoose')
  const passport = require("passport")
  // const teamRoutes = express.Router()
  const PORT = 4000
  
  //app imports
  const { teamRouter } = require('./routers')
  const { playerRouter } = require('./routers')
  const { reportRouter } = require('./routers')
  const { likeRouter } = require('./routers')
  const { leagueRouter } = require('./routers')
  const { filterRouter } = require('./routers')
  const { commentRouter } = require('./routers')
  const { adminUserRouter } = require('./routers')
  const { tokenRouter } = require('./routers')
  
  let Team = require('./models/Team')
  
  app.use(cors())
  app.use(bodyParser.json())
  
  mongoose.connect('mymongodb', { useNewUrlParser: true})
  const connection = mongoose.connection
  
  connection.once('open', function(){
      console.log('MongoDB database connection established!')
  })
  
  // Passport middleware
  app.use(passport.initialize());
  // Passport config
  require("./config/passport")(passport);
  
  
  app.use('/teams', teamRouter)
  app.use('/players', playerRouter)
  app.use('/reports', reportRouter)
  app.use('/likes', likeRouter)
  app.use('/leagues', leagueRouter)
  app.use('/filters', filterRouter)
  app.use('/comments', commentRouter)
  app.use('/api/users', adminUserRouter)
  app.use('/tokens', tokenRouter)
  
  
  app.listen(PORT, function() {
      console.log('Server is running on Port: ' + PORT)
  })
  </code></pre>
  
  <p>This is my package.json:</p>
  
  <pre><code>{
    "name": "backend",
    "version": "1.0.0",
    "description": "",
    "scripts": {
      "start": "node main.js"
    },
    "keywords": [],
    "author": "",
    "license": "ISC",
    "dependencies": {
      "axios": "^0.19.0",
      "bcryptjs": "^2.4.3",
      "body-parser": "^1.19.0",
      "concurrently": "^4.1.1",
      "cors": "^2.8.5",
      "expo-server-sdk": "^3.3.0",
      "express": "^4.17.1",
      "grunt": "^1.0.4",
      "is-empty": "^1.2.0",
      "jsonwebtoken": "^8.5.1",
      "mongoose": "^5.6.4",
      "mongoose-deep-populate": "^3.1.1",
      "nodemailer": "^6.3.0",
      "passport": "^0.4.0",
      "passport-jwt": "^4.0.0",
      "validator": "^11.1.0"
    }
  }
  </code></pre>
  
  <p>My main.js used to be called server.js, but I changed it based on the recommendation of this post:
  <a href="https://stackoverflow.com/questions/22795357/502-bad-gateway-deploying-express-generator-template-on-elastic-beanstalk">502 Bad Gateway Deploying Express Generator Template on Elastic Beanstalk</a></p>
  
  <p>Edit:
  This is my file path:</p>
  
  <p><a href="https://i.stack.imgur.com/4quGh.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/4quGh.png" alt="enter image description here"></a></p>
  
  <p>Edit:
  This is my .elasticbeanstalk config.yml file:</p>
  
  <pre><code>branch-defaults:
    default:
      environment: news-arg-backend
      group_suffix: null
  global:
    application_name: news-arg-backend
    branch: null
    default_ec2_keyname: null
    default_platform: Node.js
    default_region: us-west-2
    include_git_submodules: true
    instance_profile: null
    platform_name: null
    platform_version: null
    profile: eb-cli
    repository: null
    sc: null
    workspace_type: Application
  </code></pre>
  
  <p>In my .ebextensions, I have a nodecommand.config and a settings.config.</p>
  
  <p>This is my nodecommad.config:</p>
  
  <pre><code>option_settings:
    - namespace: aws:elasticbeanstalk:container:nodejs
      option_name: NodeCommand
      value: "npm start"
  </code></pre>
  
  <p>This is my settings.config:</p>
  
  <pre><code>option_settings:
    aws:elasticbeanstalk:container:nodejs:staticfiles:
      option_name: /static/
      value: /public/
  </code></pre>