SQLITE_CANTOPEN: unable to open database file


I am trying to run an app on my local machine but everytime I run it after packaging it throws me following error:

Uncaught Exception:
Error: SQLITE_CANTOPEN: unable to open database file
at Error (native)

If I run it through command line npm start then it works fine.

Here is my code:

var sqlite3 = require('sqlite3').verbose();
var db = new sqlite3.Database('./todo.db');
var http = require('http');
var dispatcher = require('httpdispatcher');
const PORT=6200;

var create_todos = "CREATE TABLE IF NOT EXISTS todos (description TEXT, done BOOL);",
  get_todos = "SELECT rowid AS id, description, done FROM todos ORDER BY done ASC, id DESC",
  add_todo = "INSERT INTO todos (description, done) VALUES (?, 0)",
  delete_todo = "DELETE from todos WHERE rowid=?",
  toggle_todo = "UPDATE todos SET description=? WHERE rowid=?";


function handleRequest(request, response){
    try {
        //log the request on console
        dispatcher.dispatch(request, response);
    } catch(err) {

//Create a server
var server = http.createServer(handleRequest);

//Lets start our server
server.listen(PORT, function(){
    //Callback triggered when server is successfully listening. Hurray!
    console.log("Server listening on: http://localhost:%s", PORT);

dispatcher.onGet("/get", function(req, res) {
    var todos = [];
      function(err, row) {
        todos.push({'id' : row.id, 'title' : row.description});
      }, function () {
    res.writeHead('200', {'Content-Type': 'application/json'});

dispatcher.onPost("/create", function(req, res) {
    const data = JSON.parse(req.body);
    db.run(add_todo, data.title);
    res.writeHead('200', {'Content-Type': 'text/plain'});

dispatcher.onPost("/update", function(req, res) {
    const data = JSON.parse(req.body);
    db.run(toggle_todo, data.title, data.id);
    res.writeHead('200', {'Content-Type': 'text/plain'});

dispatcher.onPost("/delete", function(req, res) {
    const data = JSON.parse(req.body);
    db.run(delete_todo, data.id);
    res.writeHead('200', {'Content-Type': 'text/plain'});

From error it looks like it is not able to find the sqllite db file, but I tried with various different paths. Same error. To generate setup file I am using following command:

electron-packager ./ todo --platform=darwin --arch=x64 --version=0.37.2


What is this path relative to? Probably not what you think it’s relative to. Instead of leaving it to chance construct an absolute path to the database file so your app doesn’t break when the working directory changes after the app is packaged.


So what should I keep the path, initially I had set it to var db = new sqlite3.Database('todo.db');
It failed


You can use the path module and __dirname to get the full path to your database.

const path = require('path')
const dbPath = path.resolve(__dirname, 'todo.db')
const db = new sqlite3.Database(dbPath)


this worked :slight_smile:


Saved the Day :slight_smile: Ty