Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

How to Generate fake data using Faker module in Node.js ?

  • Last Updated : 16 Aug, 2021

Faker module is used to generate fake data, not only fake data, infect well organized fake data. Faker module is a widely used NPM module that can generate fake names, addresses, product names, prices with all these you can also generate fake IP addresses, images, and much more using this faker package.
Command to install faker module: 
 

npm install faker

There are some predefined context for which fake data are created for a particular context as mentioned below:

 

Context(object)Aspect(method)
namefirstName, lastName, findName, suffix, jobTitle, jobDiscriptor
addresslatitude, longitude, country, state, city, zipCode, streetName
commerceproduct, productName, price, productMaterial
financeaccount, accountName, amount, currencyName, currencyCode, transactionType
imagepeople, nature, sports, animals, fashion, food, nightLife
internetemail, url, ip, mac, password, domainName

Syntax to get fake data 
 

faker.context.aspect()

Example 1: 
 



javascript




// Program to generate some fake
// names with their job titles
  
// Requiring faker module
const faker = require('faker')
  
for(let i=0; i<8; i++){
 
// Fake first name
  const firstName = faker.name.firstName()
 
// Fake last name
  const lastName = faker.name.lastName()
 
// Fake suffix
  const suffix = faker.name.suffix()
 
// Fake job Title
  const jobTitle = faker.name.jobTitle()
  
  console.log(`${suffix} ${firstName}
    ${lastName} works as '${jobTitle}'`)
}

Output: 
 

Example 2: 
 

javascript




// Program to generate some fake
// products with their details
 
// Requiring faker module
const faker = require('faker')
 
for (let i = 0; i < 8; i++) {
 
    // Fake product name
    const product = faker.commerce.product()
    // fake price of that product
    const price = faker.commerce.price()
 
    // Fake details
    const productMaterial =
        faker.commerce.productMaterial()
    console.log(`${product} made with
        ${productMaterial}, price ${price}$`)
}

Output: 
 

Example 3: 
 



javascript




// Program to generate some fake
// bank transaction details
 
// Requiring faker module
const faker = require('faker')
 
for (let i = 0; i < 8; i++) {
 
    // Fake account type
    const ac = faker.finance.account()
 
    // Fake account name
    const acName = faker.finance.accountName()
 
    // Fake transaction type
    const tT = faker.finance.transactionType()
 
    // Fake amount transaction
    const amt = faker.finance.amount()
 
    console.log(`${acName}, Account No-${ac},
    transaction Type-${tT}, Amount-${amt}`)
}

Output: 
 

Example 4: 
 

javascript




// Program to generate some fake
// domain name and ip addresses
 
// Requiring faker module
const faker = require('faker')
 
for (let i = 0; i < 8; i++) {
 
    // Fake ip address
    const ip = faker.internet.ip()
 
    // Fake domain name
    const domainName =
        faker.internet.domainName()
 
    console.log(`Domain name ->
        ${domainName}, ip-address-> ${ip}`)
}

Output: 
 

Reference: NPM faker package
 




My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!