Dirty Compact
raw JSON → 0.0.2 verified Thu Apr 23 auth: no javascript abandoned
dirty-compact is a utility package designed to compact databases created by node-dirty, a flat-file, newline-separated JSON key-value store. Last published over 12 years ago at version 0.0.2, it is tightly coupled with node-dirty, which itself has seen no updates since 2014. Both packages are long-abandoned, meaning they receive no active development, bug fixes, or security patches. They were intended for small-scale applications, typically under 1 million records, and lack modern features like ES module support, TypeScript types, or robust error handling. Due to their age and unmaintained status, they are highly susceptible to compatibility issues with current Node.js versions and present significant security vulnerabilities.
Common errors
error Error [ERR_REQUIRE_ESM]: require() of ES Module C:\path\to\node_modules\dirty-compact\index.js from C:\path\to\your_app\index.js not supported. Instead change the require of index.js in C:\path\to\your_app\index.js to a dynamic import() which is also supported but an ES module file can only be loaded as an ES module ↓
cause Attempted to `import` `dirty-compact` in an ES module context, but the package only provides CommonJS exports.
fix
Change your import statement to
const compact = require('dirty-compact').compact; and ensure your file is treated as CommonJS (e.g., .js extension with "type": "commonjs" in package.json, or an older Node.js version). error TypeError: compact is not a function ↓
cause The `compact` function was not correctly destructuring or accessing the module export, likely due to a mistaken default import or incorrect property access.
fix
Ensure you are accessing the named export
compact correctly: const { compact } = require('dirty-compact'); or const dirtyCompact = require('dirty-compact'); const compact = dirtyCompact.compact;. error Error: EACCES: permission denied, open './database.db' ↓
cause The Node.js process does not have sufficient read/write permissions for the database file or the directory it resides in.
fix
Verify that the user running the Node.js application has the necessary file system permissions (read and write) for both the source and backup database files and their containing directories.
Warnings
breaking This package, and its underlying dependency `node-dirty`, are both long-abandoned. They have not received updates or bug fixes for over a decade. Running them on modern Node.js versions is highly likely to encounter compatibility issues. ↓
fix Avoid using this package for new projects. Consider modern, actively maintained alternatives for key-value storage or flat-file databases.
breaking The package only supports CommonJS `require()` syntax. It does not provide ES module exports and will throw an `ERR_REQUIRE_ESM` error if used with `import` statements in an ES module context. ↓
fix Ensure your project is configured for CommonJS, or manually `require()` the module in an older Node.js environment. For new projects, use modern packages with ESM support.
gotcha Due to its unmaintained status, this package is vulnerable to undiscovered security flaws. There will be no patches for any reported or future vulnerabilities, making it a significant security risk for any production application. ↓
fix Do not use in production environments. If absolutely necessary for legacy systems, isolate it heavily and consider alternatives for data storage.
gotcha The underlying `node-dirty` database has documented scalability limitations, performing poorly or hitting a "hard wall" after approximately 1 million records. This compacting utility does not bypass these inherent limitations. ↓
fix Evaluate your data storage needs. For more than ~1 million records, or for any production use, choose a robust, actively maintained database solution.
gotcha Working with unmaintained file-based databases carries a high risk of data corruption, especially during compaction operations if not handled carefully or if unexpected errors occur. There is no ongoing support to address such issues. ↓
fix Always back up your database files before running any compaction. Implement robust error handling and verification steps if this package must be used.
Install
npm install dirty-compact yarn add dirty-compact pnpm add dirty-compact Imports
- compact wrong
import { compact } from 'dirty-compact';correctconst compact = require('dirty-compact').compact; - default wrong
import dirtyCompact from 'dirty-compact';correctconst dirtyCompact = require('dirty-compact'); const compact = dirtyCompact.compact;
Quickstart
const fs = require('fs');
const path = require('path');
// Create a dummy dirty database file for demonstration
const dbPath = path.join(__dirname, 'database.db');
const bakPath = path.join(__dirname, 'database.db.bak');
const dummyData = [
{ key: 'user1', value: { name: 'Alice', age: 30 } },
{ key: 'user2', value: { name: 'Bob', age: 25 } },
{ key: 'user1', value: { name: 'Alice', age: 31 } }, // Overwritten, will be compacted
{ key: 'user3', value: { name: 'Charlie', age: 35 } }
].map(entry => JSON.stringify(entry)).join('\n') + '\n';
fs.writeFileSync(dbPath, dummyData);
console.log('Created dummy database for compaction.');
// Import and use dirty-compact
const compact = require('dirty-compact').compact;
compact(dbPath, bakPath, function (err) {
if (err) {
console.error('Compaction failed:', err);
// Clean up dummy files on error for retry
if (fs.existsSync(dbPath)) fs.unlinkSync(dbPath);
if (fs.existsSync(bakPath)) fs.unlinkSync(bakPath);
return;
}
console.log('Success! Database compacted.');
// Verify compacted content (user1 should only appear once)
const compactedContent = fs.readFileSync(dbPath, 'utf8');
console.log('Compacted DB content:\n', compactedContent);
// Clean up dummy files after demonstration
if (fs.existsSync(dbPath)) fs.unlinkSync(dbPath);
if (fs.existsSync(bakPath)) fs.unlinkSync(bakPath);
console.log('Cleaned up dummy files.');
});