While working with a SQLite3 database in Node using KNEX and Objection I encountered this error:
UnhandledPromiseRejectionWarning: Error: SQLITE_CANTOPEN: unable to open database file
Seems that using relative paths isn’t a good idea. Setting the absolute path to the SQLite file instead resolves this using Node’s __dirname variable. From there simply construct the rest of your path to the database.
Your knexfile.js file can stay as-is, where you specify the path to where the database should be created. Instead look at the file you use to setup Knex/Objection and specify the absolute path there. For example:
const { Model } = require('objection'); const Knex = require('knex'); const knex = Knex({ client: 'sqlite3', useNullAsDefault: true, connection: { filename: `${__dirname}/path/to/db.sqlite3`, }, });
Another error related to not finding the database is one which claims to not being able to find the specified table when in fact it does exist.
UnhandledPromiseRejectionWarning: Error: SQLITE_ERROR: no such table: [table name]
As above, set the absolute path to the SQLite file.