How to deploy adonisjs api to Heroku


#1

Hi everyone
This is my first time deploying adonisjs api to Heroku
I successfully deployed my express app to heroku but I got error with deploying adonisjs to heroku
This is error form my terminal heroku logs --tail

2018-08-09T12:11:29.616096+00:00 heroku[web.1]: State changed from crashed to starting
2018-08-09T12:11:34.926434+00:00 heroku[web.1]: Starting process with command `npm start`
2018-08-09T12:11:37.407561+00:00 app[web.1]: 
2018-08-09T12:11:37.407580+00:00 app[web.1]: > adonis-api-app@4.1.0 start /app
2018-08-09T12:11:37.407583+00:00 app[web.1]: > node server.js
2018-08-09T12:11:37.407584+00:00 app[web.1]: 
2018-08-09T12:11:37.818354+00:00 app[web.1]: 
2018-08-09T12:11:37.818378+00:00 app[web.1]: Error: ENOENT: no such file or directory, open '/app/.env'
2018-08-09T12:11:37.818380+00:00 app[web.1]: 
2018-08-09T12:11:37.818382+00:00 app[web.1]: 
2018-08-09T12:11:37.818384+00:00 app[web.1]: 1 Env.load
2018-08-09T12:11:37.818386+00:00 app[web.1]: /app/node_modules/@adonisjs/framework/src/Env/index.js:101
2018-08-09T12:11:37.818387+00:00 app[web.1]: 
2018-08-09T12:11:37.818389+00:00 app[web.1]: 2 new Env
2018-08-09T12:11:37.818391+00:00 app[web.1]: /app/node_modules/@adonisjs/framework/src/Env/index.js:42
2018-08-09T12:11:37.818393+00:00 app[web.1]: 
2018-08-09T12:11:37.818395+00:00 app[web.1]: 3 Object.app.singleton [as closure]
2018-08-09T12:11:37.818398+00:00 app[web.1]: /app/node_modules/@adonisjs/framework/providers/AppProvider.js:29
2018-08-09T12:11:37.818400+00:00 app[web.1]: 
2018-08-09T12:11:37.818402+00:00 app[web.1]: 4 Ioc._resolveBinding
2018-08-09T12:11:37.818404+00:00 app[web.1]: /app/node_modules/@adonisjs/fold/src/Ioc/index.js:231
2018-08-09T12:11:37.818406+00:00 app[web.1]: 
2018-08-09T12:11:37.818408+00:00 app[web.1]: 5 Ioc.use
2018-08-09T12:11:37.818409+00:00 app[web.1]: /app/node_modules/@adonisjs/fold/src/Ioc/index.js:731
2018-08-09T12:11:37.818411+00:00 app[web.1]: 
2018-08-09T12:11:37.818413+00:00 app[web.1]: 6 AppProvider.boot
2018-08-09T12:11:37.818414+00:00 app[web.1]: /app/node_modules/@adonisjs/framework/providers/AppProvider.js:322
2018-08-09T12:11:37.818416+00:00 app[web.1]: 
2018-08-09T12:11:37.818418+00:00 app[web.1]: 7 _.filter.map
2018-08-09T12:11:37.818419+00:00 app[web.1]: /app/node_modules/@adonisjs/fold/src/Registrar/index.js:147
2018-08-09T12:11:37.818421+00:00 app[web.1]: 
2018-08-09T12:11:37.818422+00:00 app[web.1]: 8 arrayMap
2018-08-09T12:11:37.818424+00:00 app[web.1]: /app/node_modules/lodash/lodash.js:639
2018-08-09T12:11:37.818426+00:00 app[web.1]: 
2018-08-09T12:11:37.827262+00:00 app[web.1]: npm ERR! code ELIFECYCLE
2018-08-09T12:11:37.827421+00:00 app[web.1]: npm ERR! errno 1
2018-08-09T12:11:37.828580+00:00 app[web.1]: npm ERR! adonis-api-app@4.1.0 start: `node server.js`
2018-08-09T12:11:37.828734+00:00 app[web.1]: npm ERR! Exit status 1
2018-08-09T12:11:37.828959+00:00 app[web.1]: npm ERR!
2018-08-09T12:11:37.829167+00:00 app[web.1]: npm ERR! Failed at the adonis-api-app@4.1.0 start script.
2018-08-09T12:11:37.829324+00:00 app[web.1]: npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
2018-08-09T12:11:37.835537+00:00 app[web.1]: 
2018-08-09T12:11:37.835723+00:00 app[web.1]: npm ERR! A complete log of this run can be found in:
2018-08-09T12:11:37.835858+00:00 app[web.1]: npm ERR!     /app/.npm/_logs/2018-08-09T12_11_37_830Z-debug.log
2018-08-09T12:11:37.910666+00:00 heroku[web.1]: Process exited with status 1
2018-08-09T12:11:38.067257+00:00 heroku[web.1]: State changed from starting to crashed
2018-08-09T12:11:29.390233+00:00 app[api]: Set DB_CONNECTION config vars by user myemail@gmail.com
2018-08-09T12:11:29.390233+00:00 app[api]: Release v6 created by user myemail@gmail.com

I tried to change DB_CONNECTION but still got those errors
Anyone can help?
And what DB and free addons should I use for testing purpose?
Thank you very much


#2

The very first error is related to missing .env file. I hope, you have your environment variables in Heroku admin area. So make sure to silent the error by starting your app as follows.

ENV_SILENT=true node server.js

#3

Ok, I’m trying now


#4

It’s working
Is it that simple? :roll_eyes:


#5

Now I’m facing another issue
How can I run adonis migrations and seed them?
I have installed @adonisjs/cli with heroku run npm i -g @adonisjs/cli
But still got this on terminal when trying to run heroku run adonis migration:refresh

Running adonis migration:refresh on ⬢ bapake-adonis... up, run.2379 (Free)
bash: adonis: command not found

#6

I tried using ace command heroku run node ace migration:refresh
It’s working but still got another error

RuntimeException: E_MISSING_DB_CONNECTION: Missing database connection {postgres}.
Make sure you define it inside config/database.js file

I have configured my Heroku admin
I installed Heroku Postgres::Database
I set DB_CONNECTION=pg in Heroku Config Vars admin page
And DATABASE_URL is set by default after installing Heroku Postgress::Database

I don’t have any idea how to fix this
Anyone can help please?


#7

It’s weird
I installed pg in heroku using this command heroku run npm install pg --save
I had it installed

Running npm install pg --save on ⬢ bapake-adonis... up, run.8049 (Free)
+ pg@7.4.3
added 14 packages in 6.901s

But when I tried to run the migration on heroku using this command heroku run DB_CONNECTION=pg node ace migration:run --force, It tried to reinstall pg and couldn’t find module ‘pg’

Running DB_CONNECTION=pg node ace migration:run --force on ⬢ bapake-adonis... up, run.7632 (Free)
Knex: run
$ npm install pg --save
Error: Cannot find module 'pg'
    at Function.Module._resolveFilename (module.js:547:15)
    at Function.Module._load (module.js:474:25)
    at Module.require (module.js:596:17)
    at require (internal/module.js:11:18)
    at Client_PG._driver (/app/node_modules/knex/lib/dialects/postgres/index.js:110:12)
    at Client_PG.initializeDriver (/app/node_modules/knex/lib/client.js:254:26)
    at Client_PG.Client (/app/node_modules/knex/lib/client.js:115:10)
    at new Client_PG (/app/node_modules/knex/lib/dialects/postgres/index.js:72:20)
    at Knex (/app/node_modules/knex/lib/index.js:60:34)
    at new Database (/app/node_modules/@adonisjs/lucid/src/Database/index.js:80:17)
    at DatabaseManager.connection (/app/node_modules/@adonisjs/lucid/src/Database/Manager.js:81:35)
    at Object.get (/app/node_modules/@adonisjs/lucid/lib/proxyGet.js:40:77)
    at Migration._makeMigrationsTable (/app/node_modules/@adonisjs/lucid/src/Migration/index.js:47:36)
    at Migration.up (/app/node_modules/@adonisjs/lucid/src/Migration/index.js:336:16)
    at MigrationRun.handle (/app/node_modules/@adonisjs/lucid/commands/MigrationRun.js:69:66)
    at Promise (/app/node_modules/@adonisjs/ace/src/Command/index.js:560:48)

When I tried to run migration without DB_CONNECTION flag heroku run node ace migration:refresh --force, It throwed E_MISSING_DB_CONNECTION exception

Running DB_CONNECTION=postgres node ace migration:refresh --force on ⬢ bapake-adonis... up, run.4349 (Free)
RuntimeException: E_MISSING_DB_CONNECTION: Missing database connection {postgres}. Make sure you define it inside config/database.js file
> More details: https://err.sh/adonisjs/errors/E_MISSING_DB_CONNECTION
    at Function.missingDatabaseConnection (/app/node_modules/@adonisjs/lucid/src/Exceptions/index.js:36:12)
    at DatabaseManager.connection (/app/node_modules/@adonisjs/lucid/src/Database/Manager.js:78:33)
    at Object.get (/app/node_modules/@adonisjs/lucid/lib/proxyGet.js:40:77)
    at Migration._makeMigrationsTable (/app/node_modules/@adonisjs/lucid/src/Migration/index.js:47:36)
    at Migration.down (/app/node_modules/@adonisjs/lucid/src/Migration/index.js:403:16)
    at MigrationReset.handle (/app/node_modules/@adonisjs/lucid/commands/MigrationReset.js:69:66)
    at Promise (/app/node_modules/@adonisjs/ace/src/Command/index.js:560:48)
    at new Promise (<anonymous>)
    at Function.exec (/app/node_modules/@adonisjs/ace/src/Command/index.js:558:12)
    at Kernel.call (/app/node_modules/@adonisjs/ace/src/Kernel/index.js:302:20)
    at MigrationRefresh.handle (/app/node_modules/@adonisjs/lucid/commands/MigrationRefresh.js:67:15)
    at Promise (/app/node_modules/@adonisjs/ace/src/Command/index.js:560:48)
    at new Promise (<anonymous>)
    at Function.exec (/app/node_modules/@adonisjs/ace/src/Command/index.js:558:12)
    at Function.commanderAction (/app/node_modules/@adonisjs/ace/src/Command/index.js:535:8)
    at Command.listener (/app/node_modules/commander/index.js:315:8)

Anyone can help please?