How to deploy adonisjs api to Heroku (part 2)


#1

Hi, everyone
I need a help
I deployed adonisjs api to Heroku but I have issue of running my table migrations there.
Basically on my machine, I’m using mysql as my DB
On Heroku, I can only use Heroku Postgres::Database as my DB since it doesn’t require card number

I have 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 keep trying to install pg and couldn’t find module ‘pg’
I don’t have any idea why knex keep trying to install something
It also happened when I run with DB_CONNECTION=sqlite, knex keep tryng to install sqlite 3 and couldn’t find the module

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?


#2

I am not sure why u r installing pg driver seperately. It should be inside your package.json file


#3

On my local machine, I’m using mysql so when I deployed to Heroku I don’t have pg installed
I tried to install pg both from heroku command and on my local machine then I repush my app to heroku
But still got those error
I don’t know why knex tried to install pg again and again when I run the migration using ace command
I also use Procfile to run Heroku release command, inside the Procfile I added migration and seed command before serve the app, but still got error of knex installing pg and couldn’t find the module

I think I should set some configuration on adonisjs but I don’t know how

Last year I deployed my express api (also with knex) to Heroku and got no errors
Then I heard about adonisjs and it’s very helpful with its lucid so I don’t need to worry about knex configuration and db connection

Thanks


#4

I believe, you are trying to do too much manually, which is causing the issue.

Let’s start step by step.

  1. First of all, if you are planning to use pg in production, then make it listed as a dependency in your package.json file. Infact do not install any dependency by hand, they all should be defined inside the package file.

  2. If you are using mysql in development, then install it as a devDependency.

  3. Make sure to create the Environment variables inside the Heroku console. At least u need following variables.

  1. Inside package.json create following scripts.
  "scripts": {
    "start": "ENV_SILENT=true node server.js",
    "migrate": "ENV_SILENT=true node ace migration:run --force"
  }

Running migrations

Once, you are app has been deployed successfully, then run get into the bash of heroku and run following command.

heroku run bash --app <app-name>

# once in
npm run migrate

NOTE: Migrations have side-effects, so be careful when running them in production


#5

Ok, I’m trying
Thank you very much for your response


#6

When I ran that command, I got this errors

~ $ npm run migrate

> adonis-api-app@4.1.0 migrate /app
> ENV_SILENT=true node ace migration:run --force


SyntaxError: Unexpected identifier


1 anonymous
  /app/node_modules/require-all/index.js:52

npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! adonis-api-app@4.1.0 migrate: `ENV_SILENT=true node ace migration:run --force`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the adonis-api-app@4.1.0 migrate script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     /app/.npm/_logs/2018-08-10T09_18_49_819Z-debug.log

I don’t know what this means


#7

You get this error, coz you have some syntax errors in your migration files. Have u doubled check that they run fine on your local machine.


#8

Last time I ran the migration and seed I was OK
I’m about to double check it right now


#9

It’s hard to tell, since the stack says, that there is a syntax error. Maybe you can share the migrations, I will try to identify the issue


#10

That’s it
I got the same error
I’m gonna fix all of my migration tables now


#11

I did some changes to config/database.js on ‘pg’ section but forgot to add semicolong after that line so this error happened

Anyway, thank you very much @virk for your response


#12

I wonder if PORT variable is used or not in my heroku admin settings
I defined it as 3333 but when I accessed it from postman using that port it took so long then I cancel the request
Then after I removed the port and just access some routes using default heroku url, it took just in ms


#13

The port is used internally by Heroku to proxy your app to the web.


#14

Understood, thank you