Next.js SSR with local SQLite: "unable to open database file"

Viewed 1580

I'm creating a small Next.js app with a local ~250MB SQLite db. I put the mydb.sqlite file in the root folder of my project, and import it using better-sqlite3:

import sqlite from 'better-sqlite3';

export const db = sqlite('./mydb.sqlite');

I have a couple of server-side rendered (SSR) routes that query data from the DB and renders them in a React page. This works well locally with next dev on localhost:3000. However, when I deploy to Vercel, and visit the same SSR page, I get the error at runtime:

ERROR   SqliteError: unable to open database file

It's worthy to note that since the DB is >100MB, I cannot commit it to git. Instead, in my build phase, I download it:

// package.json
"scripts": {
    "build": "curl $DB_URL -o mydb.sqlite && next build",
},

I also have some statically-generated (SSG) pages which query the DB, and those pages are correctly generated. Only SSR pages have that error at runtime.

Is there something I forgot to do? Must I commit the db to git? Should I configure the webpack config?

I looked on the internet for sqlite + next.js, but it always involve prisma; I prefer plain SQL queries.

1 Answers

SSR pages on vercel are rendered by their serverless functions. These functions don't have your sqllite file layered inside and thus you get the unable to open error.

Related