I've stumbled upon some issues when deploying to AWS beanstalk. At first I thought the problem is with my Node.js app, but then I've tried clean install. I've created blank Node.js Express app with WebStorm. Created new Application at AWS Beanstalk running on nginx 1.6.2 Then I zip my app and deploy it through Beanstalk console. Upload completes successfully, however when I run application i get 502 bad gateway. I've tried to change the default port since Web Storm creates app running on port 3000, so I've changed it to 8081. But i still get the error. When looking up for error in logs I've got the following.
/var/log/nodejs/nodejs.log
at Function.Module._resolveFilename (module.js:338:15)
at Function.Module._load (module.js:280:25)
at Module.require (module.js:364:17)
at require (module.js:380:17)
at Object.<anonymous> (/var/app/current/app.js:8:14)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)module.js:340
throw err;
Error: Cannot find module '/var/app/current/routes/index'
at Function.Module._resolveFilename (module.js:338:15)
at Function.Module._load (module.js:280:25)
at Module.require (module.js:364:17)
at require (module.js:380:17)
at Object.<anonymous> (/var/app/current/app.js:8:14)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
module.js:340
It's clear that the problem is because node fails to find modules because of the relative path so I changed all entries with relative paths to use absolute path. for Example
var app = require('../app');
I've changed to
var path = require('path');
var app = require(path.join(__dirname, '../app'));
Still I get the same error.
Also I get the following error in nginx log:
/var/log/nginx/error.log
-------------------------------------
2014/12/05 14:28:35 [error] 23444#0: *7 connect() failed (111: Connection refused) while connecting to upstream, client: 172.31.25.31, server: , request: "GET / HTTP/1.1", upstream: "http://127.0.0.1:8081/", host: "nodesampleapp-env.elasticbeanstalk.com"
Any idea why this is happening? Appreciate any help.