Heroku Deployment

Hi! I followed step by step this article on how to deploy a strapi project to Heroku:
enHeroku Deployment

Despite that, when opening the website, I get an application error message.
Heroku logs says:
at=error code=H10 desc=“App crashed”

Anyone has any idea what to do?

Have you checked the logs in the console or terminal by using heroku logs --tail?
I had the same problem when I deployed Strapi. I found that I couldn’t connect to the database because of unsecure connection (ssl).

I had to add this in database.js to make it to work.

ssl: {
  rejectUnauthorized: false
}

config/env/production/database.js

const parse = require('pg-connection-string').parse;
const config = parse(process.env.DATABASE_URL);

module.exports = ({ env }) => ({
  connection: {
    client: 'postgres',
    connection: {
      host: config.host,
      port: config.port,
      database: config.database,
      user: config.user,
      password: config.password,
      ssl: {
        rejectUnauthorized: false
      },
    },
    debug: false,
  },
});

You also need to add the APP_KEYS from the local env file to your heroku config vars.

I added all of them from .env but it worked with only the APP_KEYS too. But better to add all if they are used in some way in strapis internal stuff.

APP_KEYS
JWT_SECRET
API_TOKEN_SALT

4 Likes

Did you add all the APP_KEYS individually or did you leave it as an array?

1 Like

The APP_KEYS is one big string in Herokus config vars. Just like it is in the .env file.

2 Likes

This is my config/env/production/database.js

const { parse } = require("pg-connection-string");

module.exports = ({ env }) => {
  const { host, port, database, user, password } = parse(env("DATABASE_URL"));

  return {
    defaultConnection: "default",
    connections: {
      default: {
        connector: "bookshelf",
        settings: {
          client: "postgres",
          host,
          port,
          database,
          username: user,
          password,
          ssl: { rejectUnauthorized: false }
        },
        options: {
          ssl: false
        },
      },
    },
  };
};

There is also my config/env/production/server.js

module.exports = ({ env }) => ({
    url: env('MY_HEROKU_URL'),
  });

The rest is the default, untouched code

Thanks guys so much! The missing keys on Heroku turned out to be the issue!

1 Like

Cool. I added everything that was listed in .env and it worked! Thanks.

1 Like

Glad I could help :+1:

Same issue here.

env/production/database.js

const parse = require("pg-connection-string").parse;
const config = parse(process.env.DATABASE_URL);
console.log(config)
module.exports = ({ env }) => ({
  connection: {
    client: "postgres",
    connection: {
      host: config.host,
      port: config.port,
      database: config.database,
      user: config.user,
      password: config.password,
      ssl: {
        rejectUnauthorized: false,
      },
      options: {
        ssl: false,
      },
    },
    debug: false,
  },
});

env/production/server.js

module.exports = ({ env }) => ({
  url: env('MY_HEROKU_URL'),
});

All env vars added in heroku

Error Log:

: > strapi start
2022-02-22T23:51:30.937969+00:00 app[web.1]: 
2022-02-22T23:51:32.299258+00:00 app[web.1]: [Object: null prototype] {
2022-02-22T23:51:32.299284+00:00 app[web.1]: user: 'uoulcpfnfnnpdptoatpd',
2022-02-22T23:51:32.299285+00:00 app[web.1]: password: '01d98982104e5c48a93e52e9c185b3b8cb216eadbf5e3fb15f38533f62a6186b9c4a2',
2022-02-22T23:51:32.299285+00:00 app[web.1]: port: '5432',
2022-02-22T23:51:32.299286+00:00 app[web.1]: host: 'ec3-32-1357-15-2328.compute-1.amazonaws.com',
2022-02-22T23:51:32.299286+00:00 app[web.1]: database: 'd993s4lb5d2s8v92m'
2022-02-22T23:51:32.299287+00:00 app[web.1]: }
2022-02-22T23:51:33.346083+00:00 heroku[web.1]: Process exited with status 1
2022-02-22T23:51:33.451697+00:00 heroku[web.1]: State changed from starting to crashed
2022-02-22T23:51:33.088204+00:00 app[web.1]: TypeError [ERR_INVALID_ARG_TYPE]: The "string" argument must be of type string or an instance of Buffer or ArrayBuffer. Received an instance of Object
2022-02-22T23:51:33.088217+00:00 app[web.1]: at new NodeError (node:internal/errors:371:5)
2022-02-22T23:51:33.088218+00:00 app[web.1]: at Function.byteLength (node:buffer:734:11)
2022-02-22T23:51:33.088218+00:00 app[web.1]: at Writer.addCString (/app/node_modules/pg-protocol/dist/buffer-writer.js:42:30)
2022-02-22T23:51:33.088219+00:00 app[web.1]: at Object.startup (/app/node_modules/pg-protocol/dist/serializer.js:10:32)
2022-02-22T23:51:33.088219+00:00 app[web.1]: at Connection.startup (/app/node_modules/pg/lib/connection.js:123:33)
2022-02-22T23:51:33.088219+00:00 app[web.1]: at Connection.<anonymous> (/app/node_modules/pg/lib/client.js:126:11)
2022-02-22T23:51:33.088220+00:00 app[web.1]: at Connection.emit (node:events:520:28)
2022-02-22T23:51:33.088220+00:00 app[web.1]: at Connection.emit (node:domain:475:12)
2022-02-22T23:51:33.088221+00:00 app[web.1]: at Socket.<anonymous> (/app/node_modules/pg/lib/connection.js:101:12)
2022-02-22T23:51:33.088221+00:00 app[web.1]: at Object.onceWrapper (node:events:640:26)
2022-02-22T23:51:33.088221+00:00 app[web.1]: at Socket.emit (node:events:520:28)
2022-02-22T23:51:33.088222+00:00 app[web.1]: at Socket.emit (node:domain:475:12)
2022-02-22T23:51:33.088222+00:00 app[web.1]: at addChunk (node:internal/streams/readable:315:12)
2022-02-22T23:51:33.088222+00:00 app[web.1]: at readableAddChunk (node:internal/streams/readable:289:9)
2022-02-22T23:51:33.088223+00:00 app[web.1]: at Socket.Readable.push (node:internal/streams/readable:228:10)
2022-02-22T23:51:33.088223+00:00 app[web.1]: at TCP.onStreamRead (node:internal/stream_base_commons:190:23)

Something with your env vars must be wrong. It say that the type is wrong.

Check all env vars if there is any double quotes that is wrong in Heroku config vars.

And get rid of the console.log before you push to heroku.

I have some problem to deploy in heroku too, in my case is the connection to the database postgress.
For some reason the /config/env/production/database.js is not override the default DB configuration on /config/database.js.

2022-02-24T09:26:18.595111+00:00 app[web.1]: > zebra-travel-dash-2@0.1.0 start
2022-02-24T09:26:18.595112+00:00 app[web.1]: > strapi start
2022-02-24T09:26:18.595112+00:00 app[web.1]: 
2022-02-24T09:26:19.104950+00:00 app[web.1]: configs:
2022-02-24T09:26:19.105201+00:00 app[web.1]: ec2-44-192-245-97.compute-1.amazonaws.com
2022-02-24T09:26:19.105217+00:00 app[web.1]: 5432
2022-02-24T09:26:19.105230+00:00 app[web.1]: de9be72grm24uv
2022-02-24T09:26:19.105243+00:00 app[web.1]: jwtxauabyfyofd
2022-02-24T09:26:19.105258+00:00 app[web.1]: d7a9dec462b728e0301814c45bc63588791098114af9f5be11910ade490c490b
2022-02-24T09:26:19.105269+00:00 app[web.1]: config end:
2022-02-24T09:26:19.930717+00:00 app[web.1]: [2022-02-24T09:26:19.930Z] debug ⛔️ Server wasn't able to start properly.
2022-02-24T09:26:19.931134+00:00 app[web.1]: [2022-02-24T09:26:19.931Z] error Error: connect ECONNREFUSED 127.0.0.1:3306
2022-02-24T09:26:19.931135+00:00 app[web.1]: at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1159:16)
2022-02-24T09:26:19.931136+00:00 app[web.1]: --------------------
2022-02-24T09:26:19.931137+00:00 app[web.1]: at Protocol._enqueue (/app/node_modules/mysql/lib/protocol/Protocol.js:144:48)
2022-02-24T09:26:19.931137+00:00 app[web.1]: at Protocol.handshake (/app/node_modules/mysql/lib/protocol/Protocol.js:51:23)
2022-02-24T09:26:19.931138+00:00 app[web.1]: at Connection.connect (/app/node_modules/mysql/lib/Connection.js:116:18)
2022-02-24T09:26:19.931139+00:00 app[web.1]: at /app/node_modules/knex/lib/dialects/mysql/index.js:68:18

and i follow all steps on the documentation,

/config/env/production/database.js

const parse = require("pg-connection-string").parse;
const { user, password, port, host, database } = parse(
  process.env.DATABASE_URL
);

console.log("configs:");
console.log(host);
console.log(port);
console.log(database);
console.log(user);
console.log(password);
console.log("config end:");

module.exports = ({ env }) => ({
  connection: {
    client: "postgres",
    connection: {
      host,
      port,
      database,
      user,
      password,
      ssl: {
        rejectUnauthorized: false,
      },
    },
    debug: true,
  },
});

there is something else we need to do?

Have you installed Postgres on Heroku and checked that is running?

If you are using Strapi v4, use the database.js below instead

const parse = require('pg-connection-string').parse;
const config = parse(process.env.DATABASE_URL);

module.exports = ({ env }) => ({
  connection: {
    client: 'postgres',
    connection: {
      host: config.host,
      port: config.port,
      database: config.database,
      user: config.user,
      password: config.password,
      ssl: {
        rejectUnauthorized: false
      },
    },
    debug: false,
  },
});

I have the same R10 issue. Development environment is working fine, on heroku i had success in general but everytime i fear.
Sometimes when opening the app i get an Error R10 (Boot timeout) → Web process failed to bind to $PORT within 60 seconds of launch. Heroku says Deploy and Build are successfull.

Heroku says Heroku dynos expose a dynamic port for your app to bind to. This value is exposed in the $PORT env var. You must change your code to bind to this port instead.
Does anyone know how can i do that in strapi v4 (4.1.8)? config/server.js looks corrent.

module.exports = ({ env }) => ({
  host: env('HOST', '0.0.0.0'),
  port: env.int('PORT', 1337),
  app: {
    keys: env.array('APP_KEYS'),
  },
});

And thats my env/production/database.js

const parse = require('pg-connection-string').parse;
const config = parse(process.env.DATABASE_URL);
module.exports = ({ env }) => ({
  connection: {
    client: 'postgres',
    connection: {
      host: config.host,
      port: config.port,
      database: config.database,
      user: config.user,
      password: config.password,
      ssl: {
        rejectUnauthorized: false
      },
    },
    debug: false,
  },
});