How to connect Node.js to SQLite
Connecting Node.js to SQLite provides a lightweight, serverless database solution perfect for development, small applications, and embedded systems.
As the creator of CoreUI with extensive Node.js development experience since 2014, I’ve used SQLite in numerous prototypes and desktop applications for simple, reliable data storage.
The most efficient approach uses the better-sqlite3 library for synchronous operations and superior performance compared to other SQLite drivers.
This method provides file-based storage with full SQL capabilities while maintaining simplicity and zero configuration requirements.
Use better-sqlite3 library to establish fast, synchronous SQLite database connections with optimal performance.
const Database = require('better-sqlite3')
const path = require('path')
class SQLiteConnection {
constructor(dbPath = './database.db') {
try {
this.db = new Database(dbPath, { verbose: console.log })
this.db.pragma('journal_mode = WAL')
this.db.pragma('foreign_keys = ON')
console.log('SQLite database connected successfully')
this.initializeTables()
} catch (error) {
console.error('SQLite connection error:', error)
throw error
}
}
initializeTables() {
const createUserTable = `
CREATE TABLE IF NOT EXISTS users (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
email TEXT UNIQUE NOT NULL,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP
)
`
this.db.exec(createUserTable)
}
insert(table, data) {
const keys = Object.keys(data)
const values = Object.values(data)
const placeholders = keys.map(() => '?').join(', ')
const stmt = this.db.prepare(
`INSERT INTO ${table} (${keys.join(', ')}) VALUES (${placeholders})`
)
return stmt.run(...values)
}
select(query, params = []) {
const stmt = this.db.prepare(query)
return stmt.all(...params)
}
selectOne(query, params = []) {
const stmt = this.db.prepare(query)
return stmt.get(...params)
}
update(query, params = []) {
const stmt = this.db.prepare(query)
return stmt.run(...params)
}
delete(query, params = []) {
const stmt = this.db.prepare(query)
return stmt.run(...params)
}
close() {
this.db.close()
console.log('SQLite connection closed')
}
}
// Usage
const db = new SQLiteConnection('./myapp.db')
// Insert data
const result = db.insert('users', {
name: 'John Doe',
email: '[email protected]'
})
console.log('User created with ID:', result.lastInsertRowid)
// Query data
const users = db.select('SELECT * FROM users WHERE email = ?', ['[email protected]'])
console.log('Users found:', users)
// Graceful shutdown
process.on('SIGTERM', () => {
db.close()
process.exit(0)
})
This code establishes a SQLite connection using better-sqlite3 with optimized settings including WAL mode for better performance and foreign key support. The connection class provides prepared statement methods for secure database operations while maintaining the simplicity that makes SQLite ideal for lightweight applications.
Best Practice Note:
This is the SQLite setup we use in CoreUI development tools and small applications for rapid prototyping and local data storage. SQLite is perfect for development environments, desktop applications, and projects that need reliable storage without server complexity.



