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

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)
name firstName, lastName, findName, suffix, jobTitle, jobDiscriptor
address latitude, longitude, country, state, city, zipCode, streetName
commerce product, productName, price, productMaterial
finance account, accountName, amount, currencyName, currencyCode, transactionType
image people, nature, sports, animals, fashion, food, nightLife
internet email, url, ip, mac, password, domainName

Syntax to get fake data

faker.context.aspect()

Example 1:



filter_none

edit
close

play_arrow

link
brightness_4
code

// 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}'`)
}

chevron_right


Output:

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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}$`)
}

chevron_right


Output:

Example 3:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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 tranaction
    const amt = faker.finance.amount()
  
    console.log(`${acName}, Account No-${ac}, 
    transaction Type-${tT}, Amount-${amt}`)
}

chevron_right


Output:

Example 4:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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}`)
}

chevron_right


Output:

Reference: NPM faker package




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.