In this article I take a look at a library which can be used to generate ‘fake' data
If your an application developer the more likely scenario is that you can use this as test data
You can use this as Node.js or in the browser but the browser does have this warning to heed on their web page
Using the browser is great for experimenting . However, due to all of the strings Faker uses to generate fake data, Faker is a large package. It's > 5 MiB
minified. Please avoid deploying Faker in your web app.
Installation
npm install @faker-js/faker --save-dev
There are various modules available in the api and these are
Module | Example |
---|---|
Address | faker.address.city() |
Animal | faker.animal.type() |
Commerce | faker.commerce.product() |
Company | faker.company.companyName() |
Database | faker.database.engine() |
Datatype | faker.datatype.uuid() |
Date | faker.date.past() |
Finance | faker.finance.amount() |
Git | faker.git.commitMessage() |
Hacker | faker.hacker.phrase() |
Helpers | faker.helpers.userCard() |
Image | faker.image.avatar() |
Internet | faker.internet.color() |
Lorem | faker.lorem.paragraph() |
Music | faker.music.genre() |
Name | faker.name.firstName() |
Phone | faker.phone.phoneNumber() |
Random | faker.random.locale() |
System | faker.system.directoryPath() |
Vehicle | faker.vehicle.vehicle() |
Each module has methods that are available. Lets take the Phone module as an example, it has the following methods
phoneNumber | Generates a random phone number |
phoneNumberFormat | Generates a random phone number with requested format (Array index) |
phoneFormats | Generates a random phone number format |
Examples
Here is a basic example which will get a random name
Save the following as fakerexample.js
const { faker } = require('@faker-js/faker'); const randomName = faker.name.findName(); console.log(`Name: ${randomName}`);
A couple of runs using node fakerexample.js
C:\Users\user\nodejs>node fakerexample.js Name: Danny Lang III C:\Users\user\nodejs>node fakerexample.js Name: Micheal Waters C:\Users\user\nodejs>node fakerexample.js Name: Nichole Dickinson
In this example lets get some random animal types
Copy this as fakeranimal.js
const { faker } = require('@faker-js/faker'); console.log (faker.animal.type()) console.log (faker.animal.dog()) console.log (faker.animal.cat()) console.log (faker.animal.snake()) console.log (faker.animal.bear()) console.log (faker.animal.lion()) console.log (faker.animal.cetacean()) console.log (faker.animal.horse()) console.log (faker.animal.bird()) console.log (faker.animal.cow()) console.log (faker.animal.fish()) console.log (faker.animal.crocodilia()) console.log (faker.animal.insect()) console.log (faker.animal.rabbit())
Run this as before
node fakeranimal.js
Here is what I saw
C:\Users\user\nodejs>node fakeranimal.js cat French Spaniel Siberian Rufous beaked snake Brown bear West African Lion Chinese White Dolphin Assateague Horse Botteri's Sparrow Sussex Japanese anchovy Saltwater Crocodile Silky ant Palomino
So as you can see from the 2 basic examples, a very useful library which has ports in other languages such as Python, Ruby and Perl
Links
https://github.com/faker-js/faker