Skip to the solution
MediumBackend6 min read

What is node:sqlite, Node's built-in SQLite module?

368 views

The problem

Question presented to candidate: "You need a local database for a small tool and do not want to add a dependency. What does Node give you now?"

What a strong answer should cover:

  • node:sqlite is a built-in module β€” introduced experimentally in Node 22, stabilising further in later releases β€” that gives you a SQLite database with zero npm dependency.
  • Its API is synchronous: DatabaseSync and StatementSync run queries and return results directly, no promises, no callbacks β€” a deliberate design choice matching SQLite's own embedded, single-process nature.
  • That makes it a natural fit for CLIs, build scripts, local caches, and tests β€” anywhere a small embedded database is useful and pulling in better-sqlite3 or a full ORM would be overkill.
  • It is not a general replacement for a real client-server database (Postgres, MySQL) in a multi-instance production service β€” SQLite's file-based, single-writer model does not fit that shape.
  • Being labelled experimental in some Node versions matters for production use: check the exact stability level of the Node version you are targeting rather than assuming it is fully stable everywhere.
  • The synchronous API is the headline trade-off to name: it blocks the event loop for the duration of the query, same as any synchronous file I/O β€” fine for small local operations, wrong for a hot request path serving concurrent users.

Clarifying questions expected:

  • "Is this for a single-process tool or a concurrently-accessed service?" β€” decides whether the synchronous model is appropriate.
  • "Which Node version, and is the feature still experimental there?"

Code / implementation expected: Optional. A short synchronous query against an in-memory database is the clearest demonstration.

  • node:sqlite
  • database
  • sqlite
  • built-in-modules

Try it yourself

Sketch your approach before you read the solution. That is what the interview tests.

Stuck? Get a hint.

A short nudge toward the approach. It will not give the answer away.

Solution

The solution is hidden so you can try first

6 min read Β· 10 sections

Read the code

JavaScriptA synchronous query against an in-memory database, no dependency
const { DatabaseSync } = require("node:sqlite");

const db = new DatabaseSync(":memory:");
db.exec("CREATE TABLE users (id INTEGER, name TEXT)");
db.prepare("INSERT INTO users (id, name) VALUES (?, ?)").run(1, "Ada");

const rows = db.prepare("SELECT * FROM users").all();
console.log(JSON.stringify(rows));
// [{"id":1,"name":"Ada"}] β€” returned directly, no await anywhere

// Verified module surface, no npm install required:
// $ node -e "console.log(Object.keys(require('node:sqlite')))"
// [ 'DatabaseSync', 'StatementSync', 'Session', 'constants', 'backup' ]

Discussion

Other approaches, follow-ups and how it went in real interviews.

Sign in to join the discussion.

No comments yet. Share how you would answer it.

Question 99 of 152 in Node.js

Keep your momentum going.

Mark this question solved to track your progress, then take the next one while it is fresh.

All Node.js questions
Up nextMedium

What is the purpose of the 'zlib' module in Node.js?

Continue