Faker.js v8 Documentation

website·Indexed 38 days ago

https://v8.fakerjs.dev/

A library for generating fake data, providing modules for creating realistic random entries such as person, address, company, finance, and internet data. Includes guides on localization, custom randomizers, and integration with testing frameworks like Vitest and Jest, as well as a migration guide for upgrading from v7 to v8.

Tokens
29.9K
Snippets
256
Records
279
Agent score
63%

What's inside Faker

  1. Overview of the Location module

    The faker.location module is used to generate addresses and location data. It replaces the faker.address module used in versions prior to 8.0.0. For standard street addresses, use a combination of streetAddress(), city(), state() (or stateAbbr()), and zipCode(). For geographic coordinates, use latitude(), longitude(), or nearbyGPSCoordinate().
  2. Overview of the faker.person module

    The faker.person module is used to generate personal information such as names and job titles. It supports locale-specific name patterns (e.g., different name orders or hyphenated names) and allows for specifying sex ('female' or 'male') to influence the generated data. For contact-specific data like phone numbers or emails, use faker.phone or faker.internet instead.
  3. Optimize Faker v8 bundle size with tree-shaking

    v8

    Faker v8 is designed to be more lightweight through two primary architectural changes:

    1. The Faker class is split into smaller units to avoid shipping entire locales when only specific strings or numbers are needed.
    2. Modules are refactored to be tree-shakeable, allowing developers to import and call individual Faker methods independently.
  4. Generate hacker and IT terminology with the Hacker module

    The faker.hacker module is used to generate random hacker and IT-specific words and phrases. It provides methods for different parts of speech (abbreviations, adjectives, nouns, verbs, and continuous verbs) as well as a method to generate complex phrases. For general vocabulary, use faker.word; for faux-Latin text, use faker.lorem; and for corporate buzzwords, use faker.company.
  5. Generate random image URLs with faker.image

    The faker.image module generates URLs pointing to images from demo providers. It does not return the image data directly, but a URL.

    • For a general random image, use url() (uses Picsum and LoremFlickr).
    • For specific providers, use urlLoremFlickr() or urlPicsumPhotos().
    • For placeholder images (solid color and text), use urlPlaceholder() or dataUri() (returns an SVG string).
    • For user avatars, use avatar().
  6. Generate random vehicle data using the Vehicle module

    The faker.vehicle module provides a variety of methods to generate automobile and bicycle data. This includes high-level vehicle names (combining manufacturer and model), specific vehicle attributes like color and fuel type, and official identifiers such as Vehicle Identification Numbers (VIN) and Vehicle Registration Marks (VRM).
  7. Faker project overview

    Faker is a library used to generate reasonable mock data for testing and development. Originally implemented in Perl in 2004, it has been ported to multiple languages including Ruby, Python, Java, Clojure, PHP, and C#. The @faker-js/faker package is the official JavaScript port maintained by a community of engineers.
  8. Use SimpleFaker for locale-independent data generation

    SimpleFaker is a simplified version of the Faker class that does not require localized data to generate output. It is ideal for scenarios where locale-specific data is unnecessary. It includes a subset of methods from the following modules: datatype, date (excluding month and weekday), helpers (excluding fake), number, and string.
    import { simpleFaker } from '@faker-js/faker';
    
    simpleFaker.number.int(10); // 4
    simpleFaker.string.uuid(); // 'c50e1f5c-86e8-4aa9-888e-168e0a182519'
  9. Retrieve locale metadata

    Use the getMetadata() method on a Faker instance to get information about the current locale, such as the language code, title, and script direction.
    import { faker, fakerES_MX } from '@faker-js/faker';
    
    faker.getMetadata(); 
    // { title: 'English', code: 'en', language: 'en', endonym: 'English', dir: 'ltr', script: 'Latn' }
    
    fakerES_MX.getMetadata(); 
    // { title: 'Spanish (Mexico)', code: 'es_MX', language: 'es', endonym: 'Español (México)', dir: 'ltr', script: 'Latn', country: 'MX' }
  10. Generate random animal-related data using the Animal module

    The faker.animal module is used to generate random animal entries. For a general animal type (e.g., 'dog'), use the type() method. For specific breeds or species, use the dedicated methods for that animal category. All generated values support localization.
  11. Generate random airline and airport data with faker.airline

    The faker.airline module provides a set of utilities to generate aviation-related data. Several methods in this module return objects rather than strings, allowing you to access specific properties (e.g., faker.airline.airport().iataCode). For passenger-related data, use the faker.person module.
  12. Migrate number methods from faker.datatype to faker.number

    Number-related methods previously in faker.datatype have moved to the new faker.number module.

    Key changes:

    • faker.datatype.number is replaced by faker.number.int or faker.number.float.
    • The default maximum for faker.number.int is now Number.MAX_SAFE_INTEGER (previously 99999).
    • faker.number.float no longer defaults to a precision of 0.01.
    // OLD
    faker.datatype.number(); // default max 99999
    faker.datatype.number({ max: 100, precision: 0.01 });
    faker.datatype.float({ max: 100 });
    
    // NEW
    faker.number.int({ max: 99999 }); // default max is now Number.MAX_SAFE_INTEGER
    faker.number.int({ max: 100 });
    faker.number.float({ max: 100 }); // precision no longer defaults to 0.01
    faker.number.float({ max: 100, precision: 0.01 });