Skip to content

Instantly share code, notes, and snippets.

@tphdev
Last active January 4, 2023 01:14
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save tphdev/2322d475d17298d05e19a9f9cb839eec to your computer and use it in GitHub Desktop.
Save tphdev/2322d475d17298d05e19a9f9cb839eec to your computer and use it in GitHub Desktop.
knex,node heroku production

Deploy Process

1 - Create an account on heroku

2 - Install heroku CLI, authenticate

npm install -g heroku
heroku login

3 - create heroku app

heroku create <your-app-name>

4 - intall postgres add on for app

https://elements.heroku.com/addons/heroku-postgresql

heroku addons:create heroku-postgresql:hobby-dev

5 - in package.json add postgress to dependencies

npm i --save pg

5 - in knexfile.js, add + export production config

// ..... after dev config .....

const prodConfig = Object.assign(
  {},
  devConfig,
  { client: 'pg', connection: process.env.DATABASE_URL}
)

module.exports = {
  development: devConfig,
  production: prodConfig
}

6 - in server.js, configure dbConnectionConfig object for knex

const dbConfigObj = require('./knexfile')

// .....

const app = express()

let dbConnectionConfig

switch (process.env.NODE_ENV){
  case 'production':
    dbConnectionConfig = dbConfigObj.production :
    break;
  default:
    dbConnectionConfig = dbConfigObj.development
}

const appDb = connectToDb(dbConnectionConfig)
Model.knex(appDb)

7 - commit your work (on master branch)

8 - push to heroku

git push heroku master

9 - run migrations

heroku run knex migrate:latest

10 - run seeds

heroku run knex seed:run
@bbrizzi
Copy link

bbrizzi commented Nov 3, 2020

Hey, do you have any idea how to automatically run the migrate script post-deploy ? I tried adding the following to my package.json but I get errors :(
"heroku-cleanup": "npx knex migrate:latest"
(No luck with heroku-postbuild either)

Thanks in advance !

@marcosvmds
Copy link

@bbrizzi

Try calling knex.migrate.latest directly in your db.js, like:

const config = require('../knexfile.js')[enviroment]
const knex = require('knex')(config)
knex.migrate.latest([config])
module.exports = knex

@maxmckenzie
Copy link

maxmckenzie commented Sep 14, 2021

Hey, do you have any idea how to automatically run the migrate script post-deploy ? I tried adding the following to my package.json but I get errors :(
"heroku-cleanup": "npx knex migrate:latest"
(No luck with heroku-postbuild either)

Thanks in advance !

I'm assuming you found out but in case anyone else looks at this.

Create a Procfile in the root of your project then you have access to a Release step

web: npm start
release: npm run some-migrations.

https://devcenter.heroku.com/articles/release-phase
https://devcenter.heroku.com/articles/procfile

@chrizzis
Copy link

What about for es modules on nodejs v16?

When I run heroku run knex migrate:latest I get the error:

require() of ES Module /app/knexfile.js from /app/node_modules/knex/lib/migrations/util/import-file.js not supported. Instead change the require of knexfile.js in /app/node_modules/knex/lib/migrations/util/import-file.js to a dynamic import() which is available in all CommonJS modules. Error [ERR_REQUIRE_ESM]: require() of ES Module /app/knexfile.js from /app/node_modules/knex/lib/migrations/util/import-file.js not supported.

It works locally, my package.json has "type": "module", and all my extensions are .js.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment