4

I want to connect to my database which is sqlite3. This is work fine on localhost and fail when I deployed to zeit.co with ERROR:

Error: SQLITE_CANTOPEN: unable to open database

As I understand, this issue is because we cannot get right path to database file. I have tried some solutions but still failed. Plz help!!!

Here is error on zeit.co log:

2020-02-13T07:50:40.031Z 4d5a9aed-ae26-4a09-903a-72aa08ca5140 ERROR Unhandled Promise Rejection {"errorType":"Runtime.UnhandledPromiseRejection","errorMessage":"Error: SQLITE_CANTOPEN: unable to open database file","reason":{"errorType":"Error","errorMessage":"SQLITE_CANTOPEN: unable to open database file","code":"SQLITE_CANTOPEN","errno":14,"stack":["Error: SQLITE_CANTOPEN: unable to open database file"]},"promise":{"isFulfilled":false,"isRejected":true,"rejectionReason":{"errorType":"Error","errorMessage":"SQLITE_CANTOPEN: unable to open database file","code":"SQLITE_CANTOPEN","errno":14,"stack":["Error: SQLITE_CANTOPEN: unable to open database file"]}},"stack":["Runtime.UnhandledPromiseRejection: Error: SQLITE_CANTOPEN: unable to open database file"," at process. (/var/runtime/index.js:35:15)"," at process.emit (events.js:228:7)"," at eventToObjectGenerator.promiseCreated (/var/task/node_modules/bluebird/js/release/debuggability.js:258:33)"," at activeFireEvent (/var/task/node_modules/bluebird/js/release/debuggability.js:301:44)"," at fireRejectionEvent (/var/task/node_modules/bluebird/js/release/debuggability.js:703:14)"," at Promise._notifyUnhandledRejection (/var/task/node_modules/bluebird/js/release/debuggability.js:90:9)"," at Timeout.unhandledRejectionCheck [as _onTimeout] (/var/task/node_modules/bluebird/js/release/debuggability.js:39:25)"," at listOnTimeout (internal/timers.js:531:17)"," at processTimers (internal/timers.js:475:7)"]} Unknown application error occurred

Here is the code that I connect to sqlite

const fs = require('fs');
const path = require('path');

const DBSOURCE = path.join(__dirname, 'mydb.sqlite');

const knex = require('knex')({
    client: 'sqlite3',
    connection: {
        filename: DBSOURCE
    }
});

Note:

  1. I have tried const DBSOURCE = 'mydb.sqlite';
  2. I have tried const DBSOURCE = path.resolve(__dirname, 'mydb.sqlite');
ROOT
  • 11,363
  • 5
  • 30
  • 45
Hoang Subin
  • 6,610
  • 6
  • 37
  • 56
  • Here is the answer: https://stackoverflow.com/questions/69669634/nextjs-post-api-not-working-in-live-environment-but-working-perfect-on-local/69671071#69671071 – Stefan Dec 08 '21 at 21:21

1 Answers1

0

For those stumbling here from Google, Zeit is now Vercel and there's an updated example for using MySQL in serverless environments.

https://github.com/vercel/next.js/tree/canary/examples/with-mysql

leerob
  • 2,876
  • 1
  • 18
  • 38