A package to generate a unique username from email or randomly selected nouns and adjectives. User can add a separator between the username, define the maximum length of a username and adds up to six random digits.
npm install unique-username-generator







A tiny, flexible username generator for Node and browsers. Generate from email or dictionaries; control separator, style, max length, optional digits, profanity filtering, templates, deterministic seeds, and batch generation.
Security note: this library generates human-friendly display names. It is not intended for security-sensitive randomness (e.g., passwords, tokens). It prefers Web Crypto when available and falls back to a non-crypto PRNG only as a last resort.

``bash`
npm install unique-username-generator --save
- Importing
`javascriptrequire()
// Using Node.js `
const { generateFromEmail, generateUsername } = require("unique-username-generator");
// Using ES6 imports
import { generateFromEmail, generateUsername } from "unique-username-generator";
It will generate username from email and add upto six random digits at the end of the name.
`javascript
// Simple: add three random digits
generateFromEmail("lakshmi.narayan@example.com", 3); // "lakshminarayan234"
// Advanced: options
generateFromEmail("123john@example.com", { randomDigits: 2, stripLeadingDigits: true }); // "john12"
generateFromEmail("12345@example.com", { randomDigits: 0, leadingFallback: "member" }); // "member"
`
It will generate unique username from adjectives, nouns, random digits and separator. You can control these following parameters - separator, number of random digits and maximum length of a username.
`javascript
// generaterUsername(separator, number of random digits, maximum length)
// Without any parameter
const username = generateUsername();
console.log(username); // blossomlogistical
// With any separator like "-, _"
const username = generateUsername("-");
console.log(username); // blossom-logistical
// With random digits and no separator
const username = generateUsername("", 3);
console.log(username); // blossomlogistical732
// With maximum length constraint and no separator, no random digits
const username = generateUsername("", 0, 15);
console.log(username); // blossomlogistic
// With maximum length constraint and separator but no random digits
const username = generateUsername("-", 0, 15);
console.log(username); // blossom-logisti
// With maximum length constraint and random digits but no separator
const username = generateUsername("", 2, 19);
console.log(username); // blossomlogistical73
// With all parameters
const username = generateUsername("-", 2, 20, "unique username");
console.log(username); // unique-username-73
`
By default, the unique username generator library comes with 2 dictionaries out of the box, so that you can use them straight away.
The new syntax for using the default dictionaries is the following:
`javascript
import { uniqueUsernameGenerator, Config, adjectives, nouns } from 'unique-username-generator';
const config: Config = {
dictionaries: [adjectives, nouns]
}
const username: string = uniqueUsernameGenerator(config); // blossomlogistical
`
You might want to provide your custom dictionaries to use for generating your unique username, in order to meet your project requirements. You can easily do that using the dictionaries option.
`javascript
import { uniqueUsernameGenerator } from 'unique-username-generator';
const marvelCharacters = [
'Iron Man',
'Doctor Strange',
'Hulk',
'Captain America',
'Thanos'
];
const config: Config = {
dictionaries: [marvelCharacters],
separator: '',
style: 'capital',
randomDigits: 3
}
const username: string = uniqueUsernameGenerator(config); // Hulk123
`
By default, the generator filters out common profane words from the built-in dictionaries to avoid unsafe names. You can extend or override the blocklist using exclude and profanityList.
`ts
import { uniqueUsernameGenerator, adjectives, nouns, DEFAULT_PROFANITY } from 'unique-username-generator';
const username = uniqueUsernameGenerator({
dictionaries: [adjectives, nouns],
exclude: ['beta', 'foo'], // custom exclusions
profanityList: DEFAULT_PROFANITY, // extend/override blocklist
randomDigits: 0
});
`
Additional styles are supported beyond lowerCase, upperCase, and capital:camelCase
- pascalCase
- kebabCase
- snakeCase
- titleCase
- (capitalize each word)
`ts`
uniqueUsernameGenerator({ dictionaries: [["blue"],["whale"]], separator: "-", style: 'camelCase', randomDigits: 0 }); // blueWhale
`ts`
// Template tokens: {adjective}, {noun}, positional {0},{1},... and {digits:n}
uniqueUsernameGenerator({
dictionaries: [adjectives, nouns],
template: "{adjective}-{noun}-{digits:2}",
seed: "v1", // make output deterministic
style: "lowerCase",
}); // e.g. "brave-otter-42"
`ts
import { generateMany, generateUniqueAsync } from 'unique-username-generator';
// Generate N (optionally unique) usernames in-memory
const many = generateMany({ dictionaries: [adjectives, nouns], count: 5, unique: true });
// Generate a username that is unique against an external store
const taken = new Set(["cool-fox"]);
const username = await generateUniqueAsync(
{ dictionaries: [adjectives, nouns], separator: "-" },
(candidate) => taken.has(candidate)
);
`
After installing, a simple CLI is available as usergen (aliases: usernamegen, unique-username, uuname).
`
Usage: usergen [options]
Options:
-s, --separator
-d, --digits
-l, --length
--style