Userito
Userito is a Node.js library designed for managing user data, offering persistence options via either a local JSON file or a MongoDB database. The current stable version is 5.0.0, released in 2022, which introduced support for Node.js 16 and dropped compatibility with older Node.js versions. While there isn't a strict release cadence, new major versions typically align with Node.js End-of-Life (EOL) cycles, dropping support for older runtimes, and incorporating dependency updates like Mongoose. Its primary differentiator is the flexibility to switch between simple file-based storage, suitable for development or small applications, and a robust MongoDB backend for more scalable solutions, all through a unified, function-based API. It provides core CRUD operations for user management.
Common errors
-
TypeError: userito.all is not a function
cause The 'userito' package exports a factory function that must be invoked with options to return an operational instance, not an object with methods directly.fixInitialize Userito by calling the imported function with configuration: `const useritoInstance = require('userito')({ type: 'file' });` or `import useritoFactory from 'userito'; const useritoInstance = useritoFactory({ type: 'file' });` -
MongoParseError: option useUnifiedTopology is not supported
cause This error typically occurs when using Mongoose v6.x (or newer) with old connection options that are no longer supported or are defaults.fixUserito v5.0.0 uses Mongoose v6.5.0. Remove deprecated options like `useNewUrlParser`, `useUnifiedTopology`, `useFindAndModify`, and `useCreateIndex` from your MongoDB connection string or `db` options, as they are no longer necessary or supported in Mongoose 6+. -
Error: Cannot find module 'userito'
cause The 'userito' package was likely installed globally (`npm i userito -g`) but not locally in your project, or the module resolution path is incorrect.fixInstall 'userito' locally in your project: `npm i userito`. If it's already installed, check your `node_modules` and module resolution paths. -
ERR_REQUIRE_ESM: require() of ES Module .../node_modules/userito/index.mjs not supported.
cause Attempting to `require()` an ESM-only package in a CommonJS context, or `userito` is incorrectly configured as ESM-only for your environment.fixIf 'userito' is indeed an ESM-only package (check its `package.json` `type` field or main entry point), ensure your project is configured for ESM (e.g., `"type": "module"` in your `package.json`) and use `import useritoFactory from 'userito';` instead of `require()`.
Warnings
- breaking Userito v5.0.0 drops support for Node.js versions older than 16. Applications running on Node.js 14 or earlier will break upon upgrade.
- breaking Userito v4.0.0 dropped support for Node.js versions older than 14. This was a significant breaking change for users on older runtimes.
- breaking Userito v3.0.0 dropped support for Node.js versions 8 and older, requiring Node.js 10 or newer. This introduced potential breaking changes for legacy applications.
- gotcha The `db` option for MongoDB initialization requires a Mongoose-compatible connection string and optionally a schema definition. Incorrect connection strings or missing schema fields can lead to database connection errors or unexpected data behavior.
- breaking Version 5.0.0 updated the underlying Mongoose dependency to `v6.5.0`. This could introduce breaking changes or require adjustments to Mongoose-specific options if your application directly interacted with Mongoose instances or relied on deprecated features from older Mongoose versions.
Install
-
npm install userito -
yarn add userito -
pnpm add userito
Imports
- userito (factory function)
const useritoInstance = require('userito'); // Missing immediate invocationconst useritoFactory = require('userito'); const useritoInstance = useritoFactory({ type: 'file' }); - userito (chained initialization)
const { useritoFile } = require('userito'); // Not a named exportconst useritoFile = require('userito')({ type: 'file' }); - userito (ESM import)
import { useritoFile } from 'userito'; // Not a named export, default export is a functionimport useritoFactory from 'userito'; const useritoFile = useritoFactory({ type: 'file' });
Quickstart
import useritoFactory from 'userito';
import path from 'path';
import { fileURLToPath } from 'url';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
async function main() {
const useritoFile = useritoFactory({
type: 'file',
path: path.join(__dirname, 'users.json') // Use a specific path for the quickstart
});
// Create a new user
await new Promise(resolve => {
useritoFile.create({ username: 'testuser', password: 'securepassword' }, (error, msg) => {
if (error) console.error('Error creating user:', error);
else console.log('User created:', msg);
resolve();
});
});
// Get all users
await new Promise(resolve => {
useritoFile.all((error, users) => {
if (error) console.error('Error getting all users:', error);
else console.log('All users:', users);
resolve();
});
});
// Get a specific user
await new Promise(resolve => {
useritoFile.get('testuser', (error, user) => {
if (error) console.error('Error getting user:', error);
else console.log('Specific user:', user);
resolve();
});
});
// Remove the user
await new Promise(resolve => {
useritoFile.remove('testuser', (error, info) => {
if (error) console.error('Error removing user:', error);
else console.log('User removed:', info);
resolve();
});
});
}
main().catch(console.error);