Node JS SQLite Crash Course

If you want to see the video version of this post, you can find it on Youtube.

If you want to see the completed source code for this post, check it out on GitHub: https://github.com/bytemyke/nodejs_sqlite_crash_course.

In this tutorial, we’ll build a simple Node.js application using SQLite.


What Is SQLite?

SQLite is a lightweight, file‑based SQL database. Unlike MySQL or PostgreSQL, it doesn’t require a server process — everything is stored in a single .db file.

SQLite is great for:

  • Learning SQL
  • Small projects
  • Prototypes
  • Desktop applications
  • Embedded systems

In this tutorial, we’ll use SQLite with Node.js to perform full CRUD(create, read, update, delete) operations:

  • Create a database
  • Create and drop tables
  • Insert data
  • Query data
  • Update data
  • Delete data

What You’ll Learn

  • How SQLite works with Node.js
  • How to create a database and tables
  • How to insert and query data
  • How to structure a basic Node + SQLite project

Project Setup

The first step to starting this project, is to hop in our terminal, create our folder and initialize a new Node.js project with SQLite installed:

You can easily open your terminal in VS code and most modern editors by pressing ctrl and ` at the same time.

Terminal
$mkdir sqlite-node
$cd sqlite-node
$npm init -y
$npm install sqlite3

Creating the App File

Next, create a new file called: app.js. This will be our main application file.

At the top of app.js, we’ll import sqlite3 and enable verbose logging.

const sqlite3 = require("sqlite3").verbose();

Always keep your require statements at the top of the file for readability.


Creating the Database File

Next, create a new file in your project directory. I'm going to name mine test.db.

This file will store all of our SQLite data.


Connecting to the Database

Create a new file called: db.js. Inside this file, we can connect to the database with the following code:

db.js
1const db = new sqlite3.Database(
2"test.db",
3sqlite3.OPEN_READWRITE,
4(err) => {
5 if (err) {
6 return console.error(err.message);
7 }
8}
9);

What’s Happening Here?

  • test.db is the database file
  • OPEN_READWRITE allows both reading and writing
  • The callback handles connection errors

This error‑handling pattern will be reused throughout the tutorial.

Run the file in your terminal.

node db.js

If there are no errors, the connection was successful.


Inserting Data

db.js
1const db = require("./db")
2
3db.run(
4"INSERT INTO users (name) VALUES (?)",
5["Alice"],
6function (err) {
7 if (err) {
8 return console.error(err.message)
9 }
10 console.log('Inserted user with ID: ' + this.lastID)
11}
12)

Querying Data

Read records from the database:

example.js
1db.all("SELECT * FROM users", [], (err, rows) => {
2if (err) {
3 throw err
4}
5
6rows.forEach(row => {
7 console.log(row)
8})
9})

Using Promises with SQLite

SQLite uses callbacks by default. You can wrap them in promises for async / await support:

example.js
1function run(db, sql, params = []) {
2return new Promise((resolve, reject) => {
3 db.run(sql, params, function (err) {
4 if (err) reject(err)
5 else resolve(this)
6 })
7})
8}
9
10function all(db, sql, params = []) {
11return new Promise((resolve, reject) => {
12 db.all(sql, params, (err, rows) => {
13 if (err) reject(err)
14 else resolve(rows)
15 })
16})
17}

When to Use SQLite

SQLite is ideal when:

  • You want a out of the box configured database
  • You’re building small apps or tools
  • You don’t want to manage or pay for a database server

Overview

As you can see, Node.js and SQLite work extremely well together. If you found this guide helpful, be sure to subscribe to my Youtube channel for more NodeJS tutorials, tips, and tricks! Happy hacking :).