lowdb is a lightweight, type-safe, local JSON database designed for Node.js, Electron, and browser environments. The current stable version is 7.0.1, with a frequent release cadence that has seen multiple major and minor versions released recently, indicating active development and continuous improvement. Key differentiators include its minimalist API for interacting with data using native JavaScript array methods, robust TypeScript support for data schema, and a highly extensible architecture via 'adapters'. These adapters allow for custom storage solutions (e.g., YAML, JSON5, encryption) and seamless integration across different environments (file system for Node, localStorage or sessionStorage for browsers). It is primarily an ESM-first package, simplifying usage in modern JavaScript projects but requiring specific import patterns. It also offers convenient presets for common use cases like JSON files and browser storage, alongside automatic in-memory mode for testing.
npm install lowdbVerified import paths — ran on the pinned version, not inferred.
Demonstrates initializing a type-safe lowdb instance with a JSON file in Node.js (ESM), adding data using `db.update`, querying data, and explicitly saving modifications made to `db.data`.
Replace `JSONPreset` with `JSONFilePreset` when importing from `lowdb/node`.
Upgrade your Node.js environment to version 18 or higher.
Ensure you always pass an initial `defaultData` object as the second argument to `new Low(adapter, defaultData)`.
Update your import paths: `import { JSONFile } from 'lowdb/node'` for Node and `import { LocalStorage } from 'lowdb/browser'` for browsers.Ensure your project is configured for ES Modules. In Node.js, add `"type": "module"` to your `package.json` or rename your files to `.mjs`. If using TypeScript, ensure your `tsconfig.json` targets an appropriate module system like `"module": "NodeNext"`.
After any direct modification to `db.data`, ensure you call `await db.write()` to persist the changes. Alternatively, use the `await db.update((data) => { /* modify data */ })` method which automatically saves and is recommended for atomic operations.Configure your project to use ES Modules. In Node.js, add `"type": "module"` to your `package.json` or rename your files to `.mjs`. If using TypeScript, ensure your `tsconfig.json` targets an appropriate module system like `"module": "NodeNext"` or `"ESNext"`.
Provide a default data object as the second argument: `new Low(adapter, { posts: [] })`.For Node.js, use `import { JSONFile } from 'lowdb/node'`. For browsers, use `import { LocalStorage } from 'lowdb/browser'`.When initializing, provide a generic type to `JSONFilePreset` or `Low` to strongly type your data: `await JSONFilePreset<MyDataType>('db.json', defaultData)` or `new Low<MyDataType>(adapter, defaultData)`.