0
votes

I deployed Strapi CMS to Heroku, but I get this error

Error connecting to the Mongo database. Server selection timed out after 30000 ms

Log:

2020-05-27T17:43:55.398256+00:00 heroku[web.1]: Starting process with command `npm start`
2020-05-27T17:43:58.724121+00:00 app[web.1]: 
2020-05-27T17:43:58.724143+00:00 app[web.1]: > [email protected] start /app
2020-05-27T17:43:58.724143+00:00 app[web.1]: > strapi start
2020-05-27T17:43:58.724143+00:00 app[web.1]: 
2020-05-27T17:44:02.234160+00:00 app[web.1]: (node:23) Warning: Accessing non-existent property 'count' of module exports inside circular dependency
2020-05-27T17:44:02.234179+00:00 app[web.1]: (Use `node --trace-warnings ...` to show where the warning was created)
2020-05-27T17:44:02.234732+00:00 app[web.1]: (node:23) Warning: Accessing non-existent property 'findOne' of module exports inside circular dependency
2020-05-27T17:44:02.234879+00:00 app[web.1]: (node:23) Warning: Accessing non-existent property 'remove' of module exports inside circular dependency
2020-05-27T17:44:02.235021+00:00 app[web.1]: (node:23) Warning: Accessing non-existent property 'updateOne' of module exports inside circular dependency
2020-05-27T17:44:32.238852+00:00 app[web.1]: [2020-05-27T17:44:32.238Z] debug ⛔️ Server wasn't able to start properly.
2020-05-27T17:44:32.253150+00:00 app[web.1]: [2020-05-27T17:44:32.253Z] error Error connecting to the Mongo database. Server selection timed out after 30000 ms

My environments settings:

database.js

enter image description here

** server.js **

enter image description here

** response.js **

enter image description here

** config vars **

enter image description here

Site works well on localhost with both dev and prod environment. So it connects to MongoDB on Atlas and no problem with that.

I do not have any addons installed on Heroku.

** packages.json **

enter image description here

On Atlas side I opened all IPs in white list.

Any idea? Thank you! :)

2
Did you manage to solve this issue? I'm currently experiencing the same problem and can't figure it out. - fraserky
Try to change in Server.js ---> Port 443 made it work. module.exports = ({ env }) => ({ host: env('HOST', '0.0.0.0'), port: env.int('PORT', 443), }); I can give you my config if it does not work. - podeig
Thanks for the reply but no luck with this. Would appreciate if you could share your config as I'm super confused why this doesn't work. My config is all on my own stack question - fraserky
I have to go now. Is it ok I answer on week end? :) - podeig
Amazing, thank you so much! Have a great weekend :) - fraserky

2 Answers

1
votes

My configuration for deployment of Strapi 3.0.1 on Heroku, both for develop and production environments:

module.exports = ({ env }) => ({
  defaultConnection: "default",
  connections: {
    default: {
      connector: "mongoose",
      settings: {
        uri: env("DATABASE_URI"),
        ssl: { rejectUnauthorized: false }
      },
      options: {
        ssl: true,
        authenticationDatabase: "",
        useUnifiedTopology: true,
        pool: {
          min: 0,
          max: 10,
          idleTimeoutMillis: 30000,
          createTimeoutMillis: 30000,
          acquireTimeoutMillis: 30000
        }
      },
    },
  },
});

server.js

module.exports = ({ env }) => ({
  host: env('HOST', '0.0.0.0'),
  port: env.int('PORT', 443), <-- 443 was critical to make it work on production
});

.env - local file

DATABASE_URI="mongodb+srv://OdegXXXXXUser:[email protected]/odeXXXXXndb?retryWrites=true&w=majority"
HOST="0.0.0.0"
PORT="1337"

Vars on Heroku:

enter image description here

DATABASE_URI er identical as on localhost, the same database.

I hope it will help to anybody :-)

0
votes

make this your database.json

{
  "defaultConnection": "default",
  "connections": {
    "default": {
      "connector": "mongoose",
      "settings": {
        "client": "mongo",
        "host": "${process.env.DATABASE_HOST}",
        "port": "${process.env.DATABASE_PORT}",
        "database": "${process.env.DATABASE_NAME}",
        "username": "${process.env.DATABASE_USERNAME}",
        "password": "${process.env.DATABASE_PASSWORD}",
      },
      "options": {}
    }
  }
}

Also make sure to add package-lock.json to your git.ignore file. then do

git add .
git commit -m "(message) "
git push heroku master

then

heroku open Start your app servers too