Creating realistic test data can often feel like a chore. Manually generating fake names, addresses, and other details takes time and can lead to inconsistencies. That’s where Faker.js comes in—a library designed to make generating fake, yet realistic, data effortless.
In this article, we'll explore what Faker.js is, where you can apply it, and how to get started with it.
screenshot of faker.js website
Faker.js is a powerful JavaScript library that generates fake data for a variety of use cases. It provides developers with access to pre-built modules for everything from names, addresses, and emails to more specialized data like company names, colors, or even product descriptions. This makes it an incredibly versatile tool for projects requiring large volumes of randomized data.
Whether you’re building forms, APIs, or user interfaces, having realistic test data can make your testing process more reliable. Faker.js ensures you aren’t testing with static or repetitive values, making it easier to simulate real-world scenarios.
Need to populate a database with realistic data for development or demos? Faker.js is your go-to tool. By generating large datasets with ease, it allows you to test your application’s performance, UI, and business logic under realistic conditions.
If your team needs to work with production-like data but privacy rules prevent the use of real user information, Faker.js can help. It enables you to replace sensitive data with randomized, realistic alternatives, ensuring compliance with privacy regulations while keeping your development workflow smooth.
Adding Faker.js to your codebase is simple, you just need to install the @faker-js/faker
package. For example, if you're using pnpm
, run the following command:
pnpm install -D @faker-js/faker
That's it! After installing, you can import Faker.js and start generating data immediately.
Here’s an example of how to use Faker.js to generate a random name:
import { faker } from '@faker-js/faker';
const randomName = faker.person.fullName();
console.log(randomName); // Outputs a realistic random name, e.g., "Jane Doe"
Faker.js also provides a variety of locales pre-configured for you. For example, if you want to generate a typical Brazilian name, you can import fakerPT_BR
.
import { fakerPT_BR as faker } from '@faker-js/faker';
const randomName = faker.person.fullName();
console.log(randomName); // A brazilian name will logged e.g., "Vitória Melo"
In my previous example, I used Faker.js to generate a person's name. To do so, I made use of the person
module. This module can produce not only names but a plethora of people-related things, including gender and bio!
faker.person.bio() // Short biography, e.g. "foodie, scientist, artist 🇹🇫"
faker.person.sex() // e.g. "male"
faker.person.gender() // e.g. "Gender fluid"
But this is just one of many modules. This is how Faker.js organizes its methods. The methods in each module are cohesive and tailored to generate data related to the module at hand.
There are a lot of modules available, you can see all of them in the API reference section of their documentation. (https://fakerjs.dev/guide/)
Some of my favorite modules are:
Let’s create a small script that generates a custom delivery details using Faker.js. This can be useful for testing an e-commerce application.
Here’s the code:
import { faker } from '@faker-js/faker';
function generateDeliveryAddress() {
return {
contact_name: faker.person.fullName(),
contact_email: faker.internet.email(),
contact_phone: faker.phone.number(),
address: faker.location.streetAddress(),
city: faker.location.city(),
state: faker.location.state(),
zipCode: faker.location.zipCode(),
};
}
console.log('Delivery Address:', generateDeliveryAddress());
When you run this script, you’ll get a randomized delivery address that looks something like this:
Delivery Address: {
contact_name: 'Ellen Pfannerstill',
contact_email: '[email protected]',
contact_phone: '1-603-439-5936 x47485',
address: '3421 Nitzsche Ferry',
city: 'Moshehaven',
state: 'Montana',
zipCode: '51029-3747'
}
There is one thing I would like to highlight about this example above.
The contact_email generated references “Noah Swaniaski”, but the contact_name mentioned the name “Ellen Pfannerstill”.
This is because I just called the faker.person.fullName()
and faker.internet.email()
methods without providing any additional arguments. Quite often, Faker.js will allow you to control the range of outputs from these methods by providing an object to the methods.
We can make the full name and email be constructed with the same first name and last name, by doing the following:
import { faker } from '@faker-js/faker';
function generateDeliveryAddress() {
const firstName = faker.person.firstName();
const lastName = faker.person.lastName();
return {
contact_name: faker.person.fullName({firstName, lastName}),
contact_email: faker.internet.email({firstName, lastName}),
// ...
};
}
//...
This script now yields an output like this:
Delivery Address: {
contact_name: 'Dr. Lila Bartell',
contact_email: '[email protected]',
contact_phone: '1-287-943-4959 x213',
address: '29591 Anderson Row',
city: 'Sophieside',
state: 'Mississippi',
zipCode: '21472-0991'
}
As you can see, now the name Lila Bartell is being used in both the email and contact name. This alignment will continue for all other results generated by our generateDeliveryAddress function.
Always check to see what options are available for each Faker.js method you're planning on using, depending on what you're trying to do, it might save you a lot of work!
Faker.js is an indispensable tool for developers working on projects that require realistic, randomized data. Whether it’s for testing, database seeding, anonymizing sensitive information, or other applications, Faker.js provides the tools to get the job done quickly and efficiently. With just a few lines of code, you can bring your test scenarios to life, making your development and testing processes smoother than ever.
If you haven’t already, give Faker.js a try in your next project. And if you want to dive deeper into how to use Faker.js checkout our video course dedicated to the topic: Generating Fake Data with Faker.js!
Happy coding!
Our goal is to be the number one source of Vue.js knowledge for all skill levels. We offer the knowledge of our industry leaders through awesome video courses for a ridiculously low price.
More than 200.000 users have already joined us. You are welcome too!
© All rights reserved. Made with ❤️ by BitterBrains, Inc.