Faker v6 Documentation

website·Indexed 38 days ago

https://v6.fakerjs.dev/

A popular library for generating fake, reasonable data for unit testing and development. Includes modules for generating names, addresses, phone numbers, finance, internet, commerce, and more. This documentation covers version v6, including migration guides from v5 and support for multiple localities.

Tokens
16K
Snippets
198
Records
203
Agent score
64%

What's inside Faker

  1. Generate random animal species and breeds using the Animal module

    The faker.animal module provides a variety of methods to generate random animal-related strings, including specific species and breeds for different categories of animals.
  2. Faker library overview and purpose

    Faker is a library used to generate reasonable mock data for testing and development purposes. While originally implemented in Perl, this specific project is the JavaScript port of the library.
  3. Generate random category-specific image URLs

    The faker.image module provides several methods to generate placeholder image URLs from specific categories using the unsplash image provider (via loremflickr). Most of these methods accept optional width (default 640), height (default 480), and a randomize boolean (default false) to append a random query string to the URL.
    // Generate an abstract image
    faker.image.abstract(1234, 2345, true); // 'http://loremflickr.com/1234/2345/abstract?56789'
    
    // Other available category methods:
    faker.image.animals();
    faker.image.business();
    faker.image.cats();
    faker.image.city();
    faker.image.fashion();
    faker.image.food();
  4. Migrate to the community-maintained Faker library

    Following the abandonment of the original Faker package in January 2022, the project was transitioned to a community-controlled effort. The official community-maintained version is now hosted under the @faker-js scope on npm.
    pm install @faker-js/faker
  5. Install @faker-js/faker

    Install Faker as a development dependency using npm, yarn, or pnpm.
    # Using npm
    npm install @faker-js/faker --save-dev
    
    # Using yarn
    yarn add @faker-js/faker --dev
    
    # Using pnpm
    pnpm add @faker-js/faker --save-dev
  6. Load specific localization packages for production

    Starting with version v3.0.0, Faker supports incremental loading of locales to reduce bundle size. While requiring the main faker package includes all locale data, you can import only the specific locale data needed for your production environment by requiring the locale-specific path.
    // loads only the German (de) locale
    const faker = require('@faker-js/faker/locale/de');
  7. Migrate from faker to @faker-js/faker package

    When migrating from Faker v5 to v6, you must replace all references to the legacy faker package with the new @faker-js/faker package namespace. This applies to both current and historical versions; for example, to install a specific historical version, use @faker-js/faker@<version>.
    npm i @faker-js/faker@5.5.3 -D
  8. Optimize Faker v6 imports for tree-shaking

    To improve bundle size and leverage tree-shaking, avoid importing the default export. Instead, use named imports for the faker object. For even better optimization (especially in the browser), import locale-specific versions directly to avoid loading the entire library of strings.
    // Recommended general import
    import { faker } from '@faker-js/faker';
    
    // Recommended locale-specific import (e.g., German)
    import fakerDe from '@faker-js/faker/locale/de';
  9. Use Faker v6 in the browser via ESM

    Faker v6 has dropped the Browser bundle in favor of ESM compatibility. To use Faker directly in the browser, include it as a JavaScript module.
    <script type="module">
      import { faker } from 'https://unpkg.com/@faker-js/faker';
    
      console.log(`${faker.name.firstName()} ${faker.name.lastName()}`);
    </script>
  10. Use Faker in the Browser

    Faker can be loaded via a script tag for experimentation. Note that Faker is a large package (> 5 MiB minified) and should not be deployed in production web applications.
    <script type="text/javascript" src="https://unpkg.com/@faker-js/faker"></script>
    
    <script>
      const randomName = faker.name.findName();
      const randomEmail = faker.internet.email();
      const randomCard = faker.helpers.createCard();
    </script>
  11. Use Faker in Node.js

    Import the faker object from the @faker-js/faker package to generate random data such as names, emails, and contact cards.
    import { faker } from '@faker-js/faker';
    
    const randomName = faker.name.findName(); // Rowan Nikolaus
    const randomEmail = faker.internet.email(); // Kassandra.Haley@erich.biz
    const randomCard = faker.helpers.createCard(); // An object representing a random contact card
  12. Generate ordinal directions with faker.address.ordinalDirection

    Returns a random ordinal direction (e.g., northwest, southeast). Use the useAbbr parameter to return abbreviated versions (e.g., NW, SE) instead of full names.
    faker.address.ordinalDirection(); // => "Southeast"
    faker.address.ordinalDirection(true); // => "NE"