Enmap
raw JSON → 6.1.6 verified Sat Apr 25 auth: no javascript
Enmap is a synchronous, persistent key-value store built on better-sqlite3, designed as a drop-in replacement for JavaScript's Map with additional array methods. Current stable version is 6.1.6, requiring Node >=20. It is TypeScript-compatible and anti-ORM, meaning it can store any serializable data without a predefined schema. Unlike typical databases or ORMs, Enmap provides a simple Map-like API with automatic persistence, making it ideal for beginners and rapid prototyping, especially in Discord bot development. It is actively maintained and synchronous, avoiding callbacks and promises.
Common errors
error Error [ERR_REQUIRE_ESM]: require() of ES Module /path/to/enmap from /path/to/yourfile.js not supported. ↓
cause Using CommonJS require() with v6 ESM-only package.
fix
Switch to import syntax: import Enmap from 'enmap'. Alternatively, downgrade to v5.x.
error TypeError: Enmap is not a constructor ↓
cause Named import instead of default import.
fix
Use import Enmap from 'enmap' (default import) instead of import { Enmap } from 'enmap'.
error Error: SQLITE_ERROR: no such table: enmap ↓
cause Attempting to use an Enmap without first creating/opening it properly.
fix
Ensure you call new Enmap({ name: 'yourname' }) before any operations.
Warnings
breaking Version 6 uses ESM only; CommonJS require() will throw an error. ↓
fix Use import syntax (ESM) or downgrade to v5.x for CommonJS support.
breaking Node.js version 20 or higher is required for v6. ↓
fix Upgrade Node.js to >=20 or use Enmap v5.x.
gotcha Enmap stores data synchronously; blocking operations can freeze the event loop on large datasets. ↓
fix For large writes, consider batching or using asynchronous alternatives like better-sqlite3 directly.
deprecated The `find()` method returns the first match, not an array, which differs from Array.prototype.find(). ↓
fix Use `filter()` or `find()` with caution; prefer `filter()` for multiple matches.
gotcha Serialization: Objects are stored as JSON, so functions, undefined, and circular references will cause errors. ↓
fix Ensure values are JSON-serializable; use custom serialization if needed.
Install
npm install enmap yarn add enmap pnpm add enmap Imports
- Enmap wrong
const Enmap = require('enmap')correctimport Enmap from 'enmap' - Enmap wrong
import { Enmap } from 'enmap'correctimport Enmap from 'enmap' - Enmap Options wrong
import Enmap, { Options } from 'enmap'correctimport Enmap from 'enmap' const enmap = new Enmap({ name: 'myEnmap' })
Quickstart
import Enmap from 'enmap';
const enmap = new Enmap({ name: 'myData' });
// Set a value
enmap.set('key1', { name: 'Alice', score: 100 });
// Get a value
const data = enmap.get('key1');
console.log(data);
// Check if key exists
if (enmap.has('key1')) {
console.log('Key exists!');
}
// Delete a key
enmap.delete('key1');
// Array methods
enmap.set('a', 1);
enmap.set('b', 2);
const sum = enmap.reduce((acc, val) => acc + val, 0);
console.log(sum); // 3
// Close the database
enmap.close();