I completed my node, postgres, angular project and is working on deployment. I recently learned about aws elastic beanstalk and for 2 days now, i have been trying unsuccessfully to deploy my project on it :( . Its been two days and really frustrating.
My project structure was like below initially
I now changed it to below after reading here that express expect .bin/www to be the starting file. I also rename my server.js to main.js Here
Despite my attempt, my project is still showing "Bad gateway 502" after deployment. I read many post about this and spent many hours.
My package.json was exactly below initially.
{
"name": "nairamarket",
"version": "1.0.0",
"description": "",
"main": "bin/www.js",
"scripts": {
"start": "NODE_ENV=production node bin/www.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
"engines": {
"node": "4.2.1",
"npm": "2.14.7"
},
"author": "",
"license": "ISC",
"dependencies": {
"body-parser": "^1.15.2",
"cookie-parser": "^1.4.3",
"email-templates": "^2.3.0",
"express": "^4.13.4",
"express-session": "^1.13.0",
"jade": "^1.11.0",
"moment": "^2.12.0",
"node-uuid": "^1.4.7",
"nodemailer": "^2.2.1",
"passport": "^0.3.2",
"passport-local": "^1.0.0",
"pg": "^4.4.4",
"pg-hstore": "^2.3.2",
"sequelize": "^3.19.2"
},
"devDependencies": {
"bower": "^1.7.7",
"nodemon": "^1.8.1",
"morgan": "^1.7.0",
"multer": "^1.1.0",
"node-sass-middleware": "^0.9.7",
}
}
After analyzing my aws log , i saw cannot find module morgan. I then realised may be the npm install does not install devs. I then move
"morgan": "^1.7.0",
"multer": "^1.1.0",
"node-sass-middleware": "^0.9.7",
TO my dependecies . The error still persist for body-parser and cookie-parser. I then disabled the two to test and see. My problem still persist. When i analysed the logs again, below is what i saw.
npm ERR! Tell the author that this fails on your system:
npm ERR! NODE_ENV=production npm install && node bin/www.js
npm ERR! You can get information on how to open an issue for this project with:
npm ERR! npm bugs nairamarket
npm ERR! Or if that isn't available, you can get their info via:
npm ERR!
npm ERR! npm owner ls nairamarket
npm ERR! There is likely additional logging output above.
And under ngix error . I noticed the error below
[error] 7987#0: *2 connect() failed (111: Connection refused) while connecting to upstream,
Below is part of my main.js and my bin/www.js just import the main.js
app.set('port', process.env.PORT || 8081);
var server = app.listen(app.get('port'), function(){
console.log('its listening', server.address().port);
debug('Express server listen on port', + server.address().port)
});
debug('PORT' + process.env.PORT);
debug('PARAM1' + process.env.PARAM1);
debug('PARAM2' + process.env.PARAM2);
debug('PARAM3' + process.env.PARAM3);
debug('PARAM4' + process.env.PARAM4);
debug('PARAM5' + process.env.PARAM5);
Please i would appreciate any tutorial or guidance with regard to this issue. Have worked on it for over two days and yet no solution.I also noticed like the node_modules/npm install is not restarting, like it has been cached . Please how do i addressed this issue and deploy my app ? Any help would be appreciated.
Update
I changed my start to
"start": "NODE_ENV=production npm install && node bin/www",
as suggested my molda below. However, it does not reflect the changes . I deployed over 3 times , yet it display node bin/www.js regardless.
I learnt that my zip or upload name need to match the name of my environment. I did do that. Now i am having the error below
fs.js:808
return binding.readdir(pathModule._makeLong(path));
And for ngix
connect() failed (111: Connection refused) while connecting to upstream,
Althougth i don't quite understood what the error implies, i am suspecting my
_driname
I have a file call root.js and it contained is below.
var path = require('path');
var dirname = path.resolve(path.resolve(__dirname));
var rootPath = path.normalize(dirname);
module.exports = rootPath;
I then reference my root.js in my config file which is two folders inward .
config.js
var rootPath = require('../../root');
I am not sure if the error is related with that but i am quite suspecting.