2

I have a MERN stack built using Facebook's Create-React-App template and Nodejs/Express that produces errors when deployed. I am using Heroku's Nodejs Buildpack

  1. At first, when I deployed it after configuring my proxy for localhost, my app would only display "Invalid Host Header". FB's docs and a Github Issue recommended specifying the host and even dangerously disabling host check; I did both of these, and error #1 was gone but #2 arose even with my .env.development file with HOST=my-heroku-app.herokuapp.com (is this wrong?). (I also tried ejecting so I could disable host check manually as described here but this also brought me to my next error.)

  2. Now I just got a Heroku application error, with the following log:

    2018-03-04T03:02:49.270931+00:00 heroku[web.1]: - Restarting
    2018-03-04T03:02:49.271285+00:00 heroku[web.1]: - State changed from up to starting
    2018-03-04T03:02:04.000000+00:00 app[api]: - Build succeeded
    2018-03-04T03:02:50.294779+00:00 heroku[web.1]: - Stopping all processes with SIGTERM
    2018-03-04T03:02:50.404316+00:00 heroku[web.1]: - Process exited with status 143
    2018-03-04T03:02:57.240490+00:00 heroku[web.1]: - Starting process with command `react-scripts start`
    2018-03-04T03:03:01.011562+00:00 heroku[web.1]: - Process exited with status 1
    2018-03-04T03:03:01.038935+00:00 heroku[web.1]: - State changed from starting to crashed
    2018-03-04T03:03:01.040756+00:00 heroku[web.1]: - State changed from crashed to starting
    2018-03-04T03:03:00.918201+00:00 app[web.1]: - Could not find an open port at myapp.herokuapp.com.
    2018-03-04T03:03:00.918218+00:00 app[web.1]: - Network error message: listen EADDRNOTAVAIL 54.235.183.213
    2018-03-04T03:03:11.873080+00:00 heroku[web.1]: - State changed from starting to crashed
    2018-03-04T03:03:11.856257+00:00 heroku[web.1]: - Process exited with status 1
    2018-03-04T03:03:14.126300+00:00 heroku[router]: - at=error code=H10 desc="App crashed" method=GET path="/" host=mysite.herokuapp.com request_id=a688b1cb-d756-4201-a056-25d65ad13de1 fwd="ip_of_my_site" dyno= connect= service= status=503 bytes= protocol=https
    

    Here's my code for setting up the server

    const port = process.env.PORT || 3001;
    //starts the server and listens for requests
    app.listen(port, function () {
        debug(`api running on port ${port}`);
    });
    

I've tried changing the port and host as suggested here with no luck.

What can I do to resolve this error in heroku? Let me know if I should provide any additional information. Thanks.

EDIT: package.json

{
  "name": "my_project",
  "version": "0.1.0",
  "private": true,
  "proxy": "http://localhost:3001/",
  "dependencies": {
    "@sendgrid/mail": "^6.2.1",
    "async": "^2.6.0",
    "aws-sdk": "^2.205.0",
    "axios": "^0.14.0",
    "body-parser": "~1.17.1",
    "cookie-parser": "~1.4.3",
    "cors": "^2.8.4",
    "debug": "^2.6.9",
    "dotenv": "^5.0.1",
    "express": "~4.15.2",
    "express-fileupload": "^0.4.0",
    "font-awesome": "^4.7.0",
    "foreman": "^2.0.0",
    "history": "^4.7.2",
    "marked": "^0.3.17",
    "milligram": "^1.3.0",
    "milligram-react": "0.0.0",
    "mongodb": "^2.2.35",
    "mongoose": "^4.13.11",
    "morgan": "~1.8.1",
    "nodemailer": "^4.6.0",
    "nodemon": "^1.17.1",
    "react": "^16.2.0",
    "react-bootstrap": "^0.32.1",
    "react-dom": "^16.2.0",
    "react-fontawesome": "^1.6.1",
    "react-icons": "^2.2.7",
    "react-onclickoutside": "^6.7.1",
    "react-router": "^4.2.0",
    "react-router-dom": "^4.2.2",
    "react-scripts": "1.0.17",
    "react-table": "^6.8.0",
    "sendgrid-web": "0.0.5",
    "serve-favicon": "~2.4.2",
    "supports-color": "^5.3.0"
  },
  "scripts": {
    "start": "react-scripts start | node server.js",
    "start-dev": "set DEBUG=* & nodemon server.js",
    "build": "react-scripts build",
    "test": "react-scripts test --env=jsdom",
    "eject": "react-scripts eject"
  }
}
abagh0703
  • 841
  • 1
  • 12
  • 26
  • Please post your `package.json` file. – Daniel Mar 05 '18 at 04:01
  • @Daniel I have posted it. – abagh0703 Mar 05 '18 at 06:23
  • Right above `"scripts"`, you need to add `"engines": { "node": "", "npm": ""`. Whenever Heroku boots up the application it will read in our `package.json` file and see if there are any versions for it to use and it will use that version whenever it deploys the application. Otherwise, Heroku tends to use an older version of `node` and `npm` and if its not your version, the app will crash. Also, I think you got too much in your start script, just do: `"scripts": "node server.js"`, although typically its `index.js`. Did you create a `.gitignore` file? – Daniel Mar 05 '18 at 13:29
  • @Daniel Thanks for the response. I did as you said and added the engines with my versions, and I edited the script to be just node server.js. I got an app crash and the same error in my heroku logs about EADDRNOTAVAIL (since I'm using the heroku nodejs buildpack I don't think my start script mattered). I have a .gitignore file, although I made sure that my .env.development file was not in the ,gitignore. – abagh0703 Mar 05 '18 at 18:42
  • So my thinking here is a process of elimination. Thank you for following through. I have noticed you have a `const host = process.env.HOST || '0.0.0.0';`. Do you need to have this at all? There seems to be a conflict with the IP address. Have you tried it without that line of code? I understand we need dynamic port binding to deploy to Heroku, but I don't think you need dynamic host binding. – Daniel Mar 05 '18 at 21:16
  • Thank you for taking the time to help. I removed the host from app.listen and unfortunately it did not result in any change in the logs or error status. If it makes any difference, on Heroku my config vars are only for my Mongo/mlab connection; not sure if I would need any others. – abagh0703 Mar 06 '18 at 11:27
  • Use (or at least reference) @mars heroku-cra-node buildpack rather than the standard Heroku-nodejs buildpack. It deploys a MERN stack for me effortlessly. – CaseyC May 17 '18 at 18:52
  • In `package.json` scripts should look more like `"scripts": { "start": "node server", "heroku-postbuild": "cd react-ui/ && npm install && npm install --only=dev --no-shrinkwrap && npm run build" },` – CaseyC May 17 '18 at 18:54

0 Answers0