How To Generate Fake Data in Node.js Applications Using Faker.js

In every field of computer science, the most important thing you require is data. Whether it’s training a machine learning model or testing your application, without data, none of the applications is possible. Moreover, it is also required to keep the data in database. But it is not guaranteed that you will get a sufficient amount of data for testing your application or all the data that you have collected will work for the test. Sometimes you also want to keep the names, emails, etc. private, so you try to create fake data for the same purpose. But creating this fake data manually will be very tedious work. So, it is great if you have an automated framework for creating fake data automatically. Faker.js do this work for you. Let’s have a glimpse at what fake.js is, how to install it, which data types it supports and how can you use faker.js in your node.js applications.

What is faker.js?

Faker.js is a JavaScript npm module that generates massive, well organized, realistic fake data for testing applications. The main benefit of faker.js is that you can use it on both server side and browser side. It is very useful when you have to work with a huge system and you have to perform many unit and integration tests. You can easily test whether the application is working correctly, by passing fake data as input and checking the output, i.e., show error if you have entered the wrong category of information, and mark the test as pass or fail.

Faker.js also gives you the benefit that you don’t need any database connection to fetch and store data. Rather, you can only use faker.js module, and using a for loop, you can get as much fake data as you want. It is a widely used npm module used to generate fake names, addresses, images, IP addresses etc. Its main advantage is that it supports giving fake data in multiple languages like French also.

Which types of data do faker.js generate?

Below is the list of categories of data that faker.js generate:

  • Names
  • Addresses
  • Images
  • Email
  • Animal
  • Vehicle
  • Phone
  • IP addresses
  • Git
  • Finance
  • Date
  • Company
  • Commerce
  • Database
  • Helpers

Every category also contains subcategories of data, some subcategories are listed below:

CategoriesSub Categories
NamefirstName, lastName, findName, suffix, jobTitle, jobDiscriptor
addresslatitude, longitude, country, state, city, zipCode, streetName
commerceproduct, productName, price, productMaterial, account, accountName, amount
imagepeople, nature, sports, food, animals etc.
internetEmail, url, ip, mac, password, domainName

The above categories and subcategories are technically named as context and aspect respectively.

How to install faker.js and how can you use it?

To run faker.js in your device, you will have to first install this npm JavaScript model by just typing the below command in your terminal:

npm install faker –save

The above code will install faker.js module on your device.

You can easily use faker.js in every purpose and to generate every category of data using the given below syntax:


Example 1: If you want to generate a fake first name using the faker.js module, you will have to type the code below:

With the help of the above code, it will give you a fake first-name of a person. If you want to extract multiple first-names then you can use for loop.

How to use faker.js to generate fake data in Node.js?

Node.js is an open-source, cross-platform, JavaScript runtime environment that executes JavaScript outside of a web browser. It is a great backend framework that is used to build data-intensive applications like streaming and real-time applications like Netflix and Uber. The main advantage of using Node.js is that it allows you to evaluate the code as a standalone application rather than depending on web browser environment. In order to test the applications, we require this faker.js module as it provides all kinds of data we require. It helps us not to interact with the tedious method of manually entering data in database. It will help you to create a faster prototype. You can easily mock your TDD/BDD tests when creating RESTful API by using this library. Let’s take a tour how you can use faker.js module in your node.js module:

Let’s first do some work on terminal to see some basic information.

1. First, we check the version of node:

$ node -v

Here, it indicates we are using Node version 11.5.0

2. Now, we initiate a new Node application

$ npm init -y

Example: Let’s create a node in which we will take fake values of name, date, address, and phone category and display them in the most used node.js functionality, i.e., console ()

const faker = require(‘faker’);
let firstName =;
let lastName =;
let suffix=;
let jobTitle=;

let weekday=;

let state= faker.address.state();
let zipCode= faker.address.zipCode();

let phone=;

console.log(‘Name: ${firstName} ${lastName} ${suffix}’);
console.log(‘Jobtitle: ${jobTitle}’);
console.log(‘Weekday: ${weekday}’);
console.log(‘State: ${state} ${zipCode}’);
console.log(‘Phone Number: ${phone}’);

Here, const faker = require(‘faker’); is used to import the faker library.

firstName =; indicates that we have generated a random first name using the faker module and stored in the firstName object. Similarly, we have done for lastName, suffix, jobtitle, weekday, state, zipcode and phone number.

We save the file as example.js

To run the file, use the command below:

$ node example.js

The output will be somewhat as shown below:

Name: Wernice Chakru II
Jobtitle: Data Analyst
Weekday: Sunday
State: Azvenia 367432
Phone Number: 1-514-716-9822


Using faker.js with node.js is a best option when you need to test your application by using fake values in order to save identity or avoid manual hard work. Faker.js has no competition when it comes to generating data for every kind of need, and you must keep exploring further how you can use it with node.js in a deep manner.

What do you think isn’t faker.js an interesting library to work with? Tell us in comments section below!!

Some links on this page are affiliate links. This means that if you choose to make a purchase, we may earn a small commission at no extra cost to you. For more information, Go here

Leave a Reply

Your email address will not be published. Required fields are marked *