'Heroku/Nodejs Failing to launch, at=error code=H10 desc="App

I know this has been asked a few times but I haven't been able to find a solution that works yet. I'm trying to have my node server deployed on Heroku and build succeeds but the log prints the following message. I'm also using mongoose and have no problem deploying locally.

2019-02-10T22:30:09.912634+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=chatifydotcom.herokuapp.com request_id=b486e511-ff21-4166-ae1d-07bf5796691e fwd="74.196.2.211" dyno= connect= service= status=503 bytes= protocol=https

2019-02-10T22:30:10.287542+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=chatifydotcom.herokuapp.com request_id=18f17ca0-56df-4dfe-a560-d890a699f17f fwd="74.196.2.211" dyno= connect= service= status=503 bytes= protocol=https

Server.js

 const uri = process.env.MONGODB_URI || "mongodb://localhost:27017/chat"
mongoose
  .connect(uri, { useNewUrlParser: true })
  .then(() => console.log("MongoDB Connected"))
  .catch(err => console.log(err.message))

    const port = process.env.PORT || 9000
server.listen(port, () => {
  console.log(`This server is over ${port}`)
})


Solution 1:[1]

I've also tried the solutions from the answers to this question

My node version is fine, my port setting is okay. None of them actually worked. My case is just a simple js script with express.

Then I tried to turn this

  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "dev": "nodemon index.js",
    "start": "node index.js"
  }

To this

  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "dev": "nodemon index",
    "start": "node index"
  }

It works.

Solution 2:[2]

I got faced the same issue and I got rid that finally. In my case I was performing git push heroku main which also main isn't the default branch. I had to go to master branch first(default branch), merge the code with my other branch and then heroku git:remote -a <repoName> and git push heroku master. HOPE this can help!

Solution 3:[3]

I was running into this issue as well. I found that it came down to a conflict in the node version being used. I am using 16.14.0; Heroku was deploying 17.5.0. As it turns out, 17.5.0 and mongoose are not getting along. I had this in my package.json:

"engines": {
  "node": ">=16.14.0",
  "npm": ">=8.3.1"
}

Making this change resolved the issue:

"engines": {
  "node": "^16.14.0",
  "npm": "^8.3.1"
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 xzifan
Solution 2 Ibrahim BHMBS
Solution 3 JonW