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) |
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:
javascript
const faker = require( 'faker' )
for (let i=0; i<8; i++){
const firstName = faker.name.firstName()
const lastName = faker.name.lastName()
const suffix = faker.name.suffix()
const jobTitle = faker.name.jobTitle()
console.log(`${suffix} ${firstName}
${lastName} works as '${jobTitle}' `)
}
|
Output:
Example 2:
javascript
const faker = require( 'faker' )
for (let i = 0; i < 8; i++) {
const product = faker.commerce.product()
const price = faker.commerce.price()
const productMaterial =
faker.commerce.productMaterial()
console.log(`${product} made with
${productMaterial}, price ${price}$`)
}
|
Output:
Example 3:
javascript
const faker = require( 'faker' )
for (let i = 0; i < 8; i++) {
const ac = faker.finance.account()
const acName = faker.finance.accountName()
const tT = faker.finance.transactionType()
const amt = faker.finance.amount()
console.log(`${acName}, Account No-${ac},
transaction Type-${tT}, Amount-${amt}`)
}
|
Output:
Example 4:
javascript
const faker = require( 'faker' )
for (let i = 0; i < 8; i++) {
const ip = faker.internet.ip()
const domainName =
faker.internet.domainName()
console.log(`Domain name ->
${domainName}, ip-address-> ${ip}`)
}
|
Output:
Reference: NPM faker package
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...