'deploying Prisma with Nuxt on heroku

I have spend several days now trying to figure out why my back-end is not working on the online version of my app. I created a Nuxt project that uses Prisma for a very small and simple database.

I had already build the "Nuxt part" of the app before actively using prisma (althought it was already implemented into the project).

After awhile I created a very basic API for prisma to work with and I got everyhting working locally then I tried to build it on heroku by adding this line: release: npx prisma migrate deploy to my Procfile.

I thought this was going to use the latest migration to recreate my local database but when navigate the online version I noticed everything that's dependent off the database isn't working because its probably not in the database.

I tried to make a migration manually and pushed the branch to see what Heroku would log and this is the result:

2021-12-30T13:46:41.573842+00:00 heroku[release.1710]: State changed from starting to up
2021-12-30T13:46:43.062707+00:00 app[release.1710]: Prisma schema loaded from prisma/schema.prisma
2021-12-30T13:46:43.083393+00:00 app[release.1710]: Datasource "db": SQLite database "dev.db" at "file:./dev.db"
2021-12-30T13:46:43.118865+00:00 app[release.1710]: 
2021-12-30T13:46:43.118897+00:00 app[release.1710]: 12 migrations found in prisma/migrations
2021-12-30T13:46:43.118939+00:00 app[release.1710]: WARNING The following migrations have been modified since they were applied:
2021-12-30T13:46:43.118940+00:00 app[release.1710]: 20211216111431_init
2021-12-30T13:46:43.118941+00:00 app[release.1710]:
2021-12-30T13:46:43.120322+00:00 app[release.1710]:
2021-12-30T13:46:43.120440+00:00 app[release.1710]: No pending migrations to apply.
2021-12-30T13:46:43.348438+00:00 heroku[release.1710]: Process exited with status 0
2021-12-30T13:46:43.426838+00:00 heroku[release.1710]: State changed from up to complete
2021-12-30T13:46:45.281380+00:00 app[api]: Release v32 created by user [email protected]
2021-12-30T13:46:46.373458+00:00 heroku[web.1]: Restarting
2021-12-30T13:46:46.454020+00:00 heroku[web.1]: State changed from up to starting
2021-12-30T13:46:47.466600+00:00 heroku[web.1]: Stopping all processes with SIGTERM
2021-12-30T13:46:47.671788+00:00 heroku[web.1]: Process exited with status 143
2021-12-30T13:46:57.249166+00:00 heroku[web.1]: Starting process with command `npm start`
2021-12-30T13:46:58.317560+00:00 app[web.1]:
2021-12-30T13:46:58.317577+00:00 app[web.1]: > [email protected] start /app
2021-12-30T13:46:58.317577+00:00 app[web.1]: > nuxt start
2021-12-30T13:46:58.317577+00:00 app[web.1]:
2021-12-30T13:46:59.566391+00:00 app[web.1]: ℹ Listening on: http://172.17.136.142:51422/
2021-12-30T13:46:59.601868+00:00 heroku[web.1]: State changed from starting to up


Solution 1:[1]

I believe it's because you're trying to deploy sqlite on Heroku, which is not recommended since Heroku will periodically clean the disk and erase your sqlite database (see SQLite on Heroku). Use their database-as-a-service instead, they have a free tier for postgresql. This would require you to also use postgresql locally since Prisma removed the ability to have multiple providers (sqlite locally and postgresql in production). It's best practice to use the same db in dev and production anyways.

Here are some steps to run postgresql locally in Nuxt 3 and deploy it to Heroku.

Locally

Create a postgres database with docker in a docker-compose.yml-file in your project root dir.

version: "3.1"

services:
  db:
    image: postgres
    restart: always
    ports:
      - "5432:5432"
    env_file:
      - .env

Create an environment variable file alongside in your project root dir, .env

POSTGRES_HOST=0.0.0.0
POSTGRES_PORT=5432
POSTGRES_DB=db
POSTGRES_USER=localadmin
POSTGRES_PASSWORD=
DATABASE_URL=postgres://localadmin:[email protected]:5432/db

For convenience, add some npm scripts to your package.json

{
  "scripts": {
    ...
    "dev": "npm run dev:db && nuxt dev",
    "dev:db": "docker-compose up -d db",
    "stop": "docker-compose down"
  }
}

Update your prisma schema.prisma and change provider to postgres

datasource db {
  provider = "postgresql"
  url      = env("DATABASE_URL")
}

Now you should be able to start local development with npm run dev and close the database container with npm run stop.

Production

Create a Heroku postgres db:

  1. Log in to Heroku and navigate to your app. Go to Resources click on Find more add-ons.
  2. Select the Heroku Postgres free tier. (3. ?) I believe Heroku adds the config vars automatically. Go to settings and Show Config Vars, there should be a DATABASE_URL with the credentials from the new Heroku Postgres db.
  3. Create a new file in your project root dir named Procfile, this tells Heroku which scripts to run on deploy. It should have the instruction for web and release to tell Heroku how to start the server and how to make migrations.
web: npm start

release: npx prisma migrate deploy

Hope that helps!

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Mattias Naarttijärvi