Faker.js Documentation

website·Indexed 30 days ago

https://fakerjsdocs.netlify.app/

A library to generate massive amounts of fake data in the browser and Node.js. Includes APIs for generating random names, addresses, dates, internet data, finance, commerce, and more, with localization support starting from version v2.0.0.

Tokens
9.2K
Snippets
109
Records
110
Agent score
74%

What's inside Faker.js

  1. Load a specific localization package incrementally

    Starting with version v3.0.0, faker.js supports incremental loading of locales. While requiring the base faker package includes all locale data, you can import only a specific locale to optimize production environments and reduce bundle size.
    // loads only the German (de) locale
    var faker = require('faker/locale/de');
  2. Set the global locale for faker.js

    As of version v2.0.0, faker.js supports multiple localities. The default locale is English. You can change the global locale using faker.setLocale() or by assigning a value to faker.locale.
    // sets locale to German
    faker.setLocale("de");
    // or
    faker.locale = "de";
  3. Use Faker.js in the browser

    To use Faker.js in a web browser, include the faker.js script in your HTML. You can then access the faker global object to generate random data such as names, emails, and contact cards.
    <script src="faker.js" type="text/javascript"></script>
    <script>
        var randomName = faker.name.findName(); // e.g., Caitlyn Kerluke
        var randomEmail = faker.internet.email(); // e.g., Rusty@arne.info
        var randomCard = faker.helpers.createCard(); // random contact card containing many properties
    </script>
  4. Use Faker.js in Node.js

    In a Node.js environment, import the faker module using require. This allows you to generate mock data for testing or development purposes.
    var faker = require('faker');
    
    var randomName = faker.name.findName(); // e.g., Rowan Nikolaus
    var randomEmail = faker.internet.email(); // e.g., Kassandra.Haley@erich.biz
    var randomCard = faker.helpers.createCard(); // random contact card containing many properties
  5. Combine Faker methods using faker.fake()

    The faker.fake(mustacheStringFormat) method allows you to combine multiple Faker API methods within a single string using a mustache template format (e.g., {{category.method}}). This is useful for creating complex, formatted strings that incorporate multiple pieces of generated data.
    // Combine name components
    faker.fake("{{name.lastName}}, {{name.firstName}} {{name.suffix}}")
    // Example output: "Wintheiser, Shaylee Sr."
    
    // Combine different categories
    faker.fake("{{company.bs}} is short for {{address.streetName}}") 
    // Example output: "cutting-edge leverage web services is short for Flatley Rue"
  6. Generate random lorem text and lines

    For general purpose placeholder text, use faker.lorem.text(), which returns random text based on a random lorem method. To generate multiple lines of text separated by newlines, use faker.lorem.lines([lineCount]), where lineCount defaults to a range of 1-5.
    faker.lorem.text(); // Returns a block of random lorem text
    
    faker.lorem.lines(); // Default 1-5 lines separated by \n
    faker.lorem.lines(5); // Exactly 5 lines separated by \n
  7. Generate random lorem words

    Use faker.lorem.word() to get a single random word, or faker.lorem.words() to generate multiple words. The words() method accepts an optional num parameter to specify the number of words to return.
    faker.lorem.word(); // e.g., 'maiores'
    faker.lorem.words(); // Default 3 words
    faker.lorem.words(6); // Generates 6 random words
  8. Generate fake data via the Hosted API Microservice

    Faker.js provides a hosted API microservice at http://faker.hook.io that supports all Faker API methods. You can request specific properties and locales via URL parameters.
    curl http://faker.hook.io?property=name.findName&locale=de
  9. Generate a random lorem slug

    Use faker.lorem.slug([wordCount]) to generate a URL-friendly slug. The wordCount parameter defaults to 3.
    faker.lorem.slug(); // e.g., 'repudiandae-magnam-voluptatem'
    faker.lorem.slug(4); // e.g., 'sed-quo-labore-aliquid'
  10. Generate random lorem paragraphs

    Use faker.lorem.paragraph() for a single paragraph or faker.lorem.paragraphs() for multiple.

    • paragraph([sentenceCount]): Generates a paragraph. sentenceCount defaults to 3.
    • paragraphs([paragraphCount, separator]): Generates multiple paragraphs. paragraphCount defaults to 3, and separator defaults to '\n \r'.
    faker.lorem.paragraph(); // Default 3 sentences
    faker.lorem.paragraph(5); // Paragraph with 5 sentences
    
    faker.lorem.paragraphs(); // Default 3 paragraphs
    faker.lorem.paragraphs(5); // 5 paragraphs
    faker.lorem.paragraphs(2, '=>'); // 2 paragraphs separated by '=>'
  11. Generate random lorem sentences

    Use faker.lorem.sentence() for a single sentence or faker.lorem.sentences() for multiple.

    • sentence([wordCount]): Generates a sentence. wordCount defaults to a range of 3-10 words.
    • sentences([sentenceCount, separator]): Generates multiple sentences. sentenceCount defaults to 2-6, and separator defaults to a space (' ').
    faker.lorem.sentence(); // Default length
    faker.lorem.sentence(3); // Sentence with 3 words
    
    faker.lorem.sentences(); // Default 2-6 sentences
    faker.lorem.sentences(1); // Exactly 1 sentence
    faker.lorem.sentences(2, '='); // 2 sentences separated by '='
  12. Generate complex mock data objects

    Faker provides several helpers to generate complete mock profiles and transaction records for testing UI components or APIs.
    // Generates a comprehensive user profile including address, company, posts, and account history
    const card = faker.helpers.createCard();
    
    // Generates a user profile with an avatar and date of birth
    const contextualCard = faker.helpers.contextualCard();
    
    // Generates a basic user profile
    const userCard = faker.helpers.userCard();
    
    // Generates a financial transaction object
    const transaction = faker.helpers.createTransaction();