Applications and operations

Use SQLite from Node.js

Open SQLite from Node.js, configure lock waits, bind values, read rows, verify foreign keys, and close the connection.

8 minute lesson

~~~

node:sqlite was added in Node.js 22.5. As of July 2026, Node marks it as a release-candidate API with stability 1.2. The example below needs a current supported Node release with the timeout option.

Create app.js:

import { DatabaseSync } from 'node:sqlite'

const db = new DatabaseSync('notes.db', { timeout: 3000 })

db.exec('PRAGMA foreign_keys = ON')
const foreignKeys = db.prepare('PRAGMA foreign_keys').get()
console.log(foreignKeys)

db.exec(`
  CREATE TABLE IF NOT EXISTS notes (
    id INTEGER PRIMARY KEY,
    title TEXT NOT NULL
  ) STRICT
`)

const insert = db.prepare('INSERT INTO notes (title) VALUES (?)')
insert.run('Plan the week')

const notes = db.prepare(
  'SELECT id, title FROM notes ORDER BY id'
).all()
console.log(notes)

db.close()

Run it with node app.js. The timeout lets a short lock conflict wait for three seconds. Current node:sqlite versions enable foreign keys by default, but setting and reading the pragma makes the application requirement explicit.

DatabaseSync runs every operation on the JavaScript thread. It fits scripts, tests, command-line tools, and light application work. Long queries or heavy concurrent traffic block that thread, so choose another architecture when the workload needs it.

Lesson completed

Take this course offline

Get every free book and course as PDF and EPUB files.

Get the download library →