wiljago
wiljago

Reputation: 146

Starting a new Sequelize backend and cannot connect to SQLite DB

I have the most basic bones of a new app.js set up with sequelize and express in node. Whenever I run the project I get:

{ Error: SQLITE_CANTOPEN: unable to open database file errno: 14, code: 'SQLITE_CANTOPEN' }

I've searched around for solutions, but I'm not using other technologies like electron that seem to cause issues, according to the other posts I found. I did also try the suggestion of moving my database.sqlite3 from the root directory into its own folder, but that did not help.

My app.js looks like a boilerplate still. I am really not doing much, just trying to test a connection after creating models and migrating using the sequelize-cli.

const express = require('express');
const bodyParser = require('body-parser');
const path = require('path');

//Database Connection
const db = require('./config/database');

const Sequelize = require("sequelize");

const sequelize = new Sequelize({
  dialect: "sqlite",
  storage: "path/to/database.sqlite"
});

//Test the DB Connection
sequelize.authenticate()
  .then(() => console.log('Database Connected'))
  .catch(err => console.log('Error: ', err))



//Server
const app = express()

app.get('/', (req, res) => res.send('HELLO WORLD'))

const PORT = process.env.PORT || 5000;

app.listen(PORT, console.log(`Server started on ${PORT}`));

My config.json is as follows, and I did double check the path.

{
  "development": {
    "dialect": "sqlite",
    "storage": "./db/database.sqlite3"
  },
  "test": {
    "dialect": "sqlite",
    "storage": ":memory"
  },
  "production": {
    "dialect": "sqlite",
    "storage": "./db/database.sqlite3"
  }
}

Upvotes: 2

Views: 6658

Answers (1)

Kryten
Kryten

Reputation: 15760

It looks like you still have boilerplate code in your file. Specifically, look at these lines:

const sequelize = new Sequelize({
  dialect: "sqlite",
  storage: "path/to/database.sqlite"
});

It doesn't look like you're using the configuration file; instead, you're trying to open a database file at path/to/database.sqlite3.

Try this instead:

const sequelize = new Sequelize(db[process.env.NODE_ENV]);

(I'm assuming you want to load the database config corresponding to the current environment).

Upvotes: 5

Related Questions