Node.js Built-in SQLite Driver
Node.js bundles a SQLite driver in node:sqlite. Open a file with new DatabaseSync(path), then run SQL with exec() or prepared statements. Use it for local tools and caches. DatabaseSync is synchronous, so running it on a web server main thread blocks requests.
Node.js applications that need structured persistence without external dependencies can use the built-in node:sqlite module. It exists to give developers a robust relational storage option that does not require installing third-party drivers or managing a separate database server process. By embedding SQLite directly into the runtime, Node.js lets scripts query data using standard SQL while keeping the deployment footprint small.
The mental model
Think of node:sqlite as a built-in bridge between JavaScript and a single-file SQL engine. You open a file handle with DatabaseSync, which creates a direct channel to execute SQL strings and retrieve converted JavaScript values. It is not a network client talking to a remote port; it is an embedded interpreter that reads and writes to a local database file on disk. The module handles translating JavaScript strings, numbers, and booleans into SQLite storage types and back again.
How it works
You import the node:sqlite module and create a DatabaseSync instance by passing a file path. The connection starts via the constructor or an explicit open call. To execute SQL, you have two main paths. First, database.exec runs a raw SQL string directly, which is useful for DDL like CREATE TABLE statements. Second, database.prepare compiles a SQL string into a StatementSync object that you can run multiple times with different parameters. Prepared statements support named parameters and anonymous parameters, and they offer methods like run for inserts and updates, get for a single row, all for every row, and iterate for sequential consumption. Results automatically convert between SQLite types and JavaScript types according to the documented type conversion rules. When finished, you call database.close to release the file handle.
When to use it
Use this module when you need lightweight structured storage without running a separate database server. It excels in command-line tools, local development environments, caching layers, embedded devices, and single-instance applications where a full client-server database would be overkill.
When not to use it
Do not use DatabaseSync in performance-sensitive network servers where query latency would block subsequent operations, because its API is synchronous. It is also unsuitable when you need many concurrent writers across multiple machines, since the module works with a single local file.
One canonical example
A logging CLI tool creates a DatabaseSync instance pointing to logs.db. It uses database.exec to create a table if it does not exist, then prepares an INSERT statement with database.prepare. On each new log entry, it calls statement.run with the timestamp and message as bound parameters, letting SQLite handle persistence and type conversion. When the script exits, it calls database.close to ensure the file handle is released cleanly.
Interview question
Given its synchronous API and embedded design, which workload is node:sqlite best suited for?
- a.A distributed system where multiple servers write to the same database file concurrently
- b.A video streaming service requiring non-blocking database access for each request
- c.A local CLI utility that logs events to a file without extra npm dependenciesCorrect
- d.A high-traffic e-commerce API processing hundreds of checkouts per second
Why? this is the answer
node:sqlite is designed for lightweight, local structured storage such as CLI tools that avoid external dependencies, whereas a high-traffic API is a poor fit because DatabaseSync executes synchronously and blocks the event loop.
Keep this idea fresh
Try a few questions or explore another bite in Node.js & Express.
Read the original → nodejs.org
Put your scrolling time to good use
Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.
The iPhone app is on the way
We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.
Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.
We are hiring for this. Open roles that interview on node.js — each one lists the topics its interview covers.
See open roles