Using generate-password in a browser
mastergenerate-password module uses Node.js require and is not compatible with web environments. For browser-based projects, use the generate-password-browser package instead.repository·master·Indexed 18 days ago
https://github.com/brendanashworth/generate-passwordA Node.js library for generating random and unique passwords. It provides the generate() method for single passwords and generateMultiple() for bulk generation, with configurable options for length, character pools (numbers, symbols, lowercase, uppercase), and character exclusion.
generate-password module uses Node.js require and is not compatible with web environments. For browser-based projects, use the generate-password-browser package instead.To use this library in your Node.js project, install it using npm:
$ npm install generate-password --saveUse the generate([options]) method to create a single password. It returns a string based on the provided configuration object.
var generator = require('generate-password');
var password = generator.generate({
length: 10,
numbers: true
});
// 'uEyMTw32v9'
console.log(password);Use the generateMultiple(amount[, options]) method to bulk generate an array of passwords using the same configuration. The amount parameter specifies how many passwords to create.
var generator = require('generate-password');
var passwords = generator.generateMultiple(3, {
length: 10,
uppercase: false
});
// [ 'hnwulsekqn', 'qlioullgew', 'kosxwabgjv' ]
console.log(passwords);The following options can be passed into the options object for both generate() and generateMultiple().
Note: At least one character pool option (numbers, symbols, lowercase, or uppercase) must be set to true for the generator to function.
| Name | Description | Default Value |
|---|---|---|
length | Integer, length of password. | 10 |
numbers | Boolean, put numbers in password. | false |
symbols | Boolean or String, put symbols in password. | false |
lowercase | Boolean, put lowercase in password. | true |
uppercase | Boolean, use uppercase letters in password. | true |
excludeSimilarCharacters | Boolean, exclude similar chars, like 'i' and 'l'. | false |
exclude | String, characters to be excluded from password. | '' |
strict | Boolean, password must include at least one character from each pool. | false |
The library is exported from the main entry point. You can require or import it to access the password generation functionality provided by the core module.
const generatePassword = require('generate-password');The GenerateOptions interface allows you to customize the properties of the generated password. You can control the length, character sets (numbers, symbols, lowercase, uppercase), and character exclusion rules.
interface GenerateOptions {
/**
* Length of the generated password.
* @default 10
*/
length?: number;
/**
* Should the password include numbers
* @default false
*/
numbers?: boolean;
/**
* Should the password include symbols, or symbols to include
* @default false
*/
symbols?: boolean | string;
/**
* Should the password include lowercase characters
* @default true
*/
lowercase?: boolean;
/**
* Should the password include uppercase characters
* @default true
*/
uppercase?: boolean;
/**
* Should exclude visually similar characters like 'i' and 'I'
* @default false
*/
excludeSimilarCharacters?: boolean;
/**
* List of characters to be excluded from the password
* @default ""
*/
exclude?: string;
/**
* Password should include at least one character from each pool
* @default false
*/
strict?: boolean;
}Use the generateMultiple function to create an array of passwords. The first argument is the count (number of passwords to generate), and the second is an optional GenerateOptions object applied to all generated passwords.
export function generateMultiple(count: number, options?: GenerateOptions): string[];