Research
Security News
Threat Actor Exposes Playbook for Exploiting npm to Build Blockchain-Powered Botnets
A threat actor's playbook for exploiting the npm ecosystem was exposed on the dark web, detailing how to build a blockchain-powered botnet.
@faker-js/faker
Advanced tools
The @faker-js/faker package is a powerful tool for generating massive amounts of fake (but realistic) data for various purposes such as testing, bootstrapping databases, and creating rich prototypes. It can create data for names, addresses, phone numbers, and much more.
Basic random data generation
Generate random names, addresses, and other basic personal information.
const { faker } = require('@faker-js/faker');
console.log(faker.name.findName()); // Generates a random name
Localization
Support for localized data, allowing generation of data that is locale-specific.
const { faker } = require('@faker-js/faker');
faker.locale = 'de';
console.log(faker.name.findName()); // Generates a random name with German locale
Random numbers
Create random numbers, including options for specifying ranges and precision.
const { faker } = require('@faker-js/faker');
console.log(faker.datatype.number()); // Generates a random number
Date and time
Generate random dates and times, with the ability to specify past or future dates.
const { faker } = require('@faker-js/faker');
console.log(faker.date.future()); // Generates a random future date
Internet-related data
Create fake internet data such as email addresses, domain names, and URLs.
const { faker } = require('@faker-js/faker');
console.log(faker.internet.email()); // Generates a random email address
Commerce data
Generate commerce-related data like product names, prices, and categories.
const { faker } = require('@faker-js/faker');
console.log(faker.commerce.productName()); // Generates a random product name
Images
Create URLs for random placeholder images.
const { faker } = require('@faker-js/faker');
console.log(faker.image.imageUrl()); // Generates a random image URL
Chance is a minimalist generator of random strings, numbers, etc. to help reduce some monotony particularly while writing automated tests or anywhere else you need anything random.
Casual is a fake data generator for JavaScript which is more lightweight than @faker-js/faker and provides an easy-to-use API with a smaller set of functionalities.
Randexp generates strings that match a given regular expression which is useful for creating random data that fits a specific pattern, unlike @faker-js/faker which provides a wide range of predefined data types.
Please replace your faker
dependency with @faker-js/faker
. This is the official, stable fork of Faker.
npm install @faker-js/faker --save-dev
or yarn
yarn add @faker-js/faker -D
or pnpm
pnpm install @faker-js/faker -D
<script src="faker.js" type="text/javascript"></script>
<script>
const randomName = faker.name.findName(); // Caitlyn Kerluke
const randomEmail = faker.internet.email(); // Rusty@arne.info
const randomPhoneNumber = faker.phone.phoneNumber(); // (746) 637-3344 x8083
</script>
const { faker } = require('@faker-js/faker');
const randomName = faker.name.findName(); // Rowan Nikolaus
const randomEmail = faker.internet.email(); // Kassandra.Haley@erich.biz
const randomPhoneNumber = faker.phone.phoneNumber(); // (279) 329-8663 x30233
import { faker } from 'https://cdn.skypack.dev/@faker-js/faker';
const randomName = faker.name.findName(); // Willie Bahringer
const randomEmail = faker.internet.email(); // Tomasa_Ferry14@hotmail.com
const randomPhoneNumber = faker.phone.phoneNumber(); // 938-672-1359 x418
esm:
cjs:
Since version v6+
there is native TypeScript support.
In order to have faker working properly, you need to check if these compilerOptions
are set correctly in your tsconfig
file:
{
"compilerOptions": {
"esModuleInterop": true,
"moduleResolution": "Node"
}
}
And then simply import it like everything else:
import { faker } from '@faker-js/faker';
If you want for whatever reason the versions prior to v6
,
you can use @types/faker
and rebind the declarations to the @faker-js/faker
package with a faker.d.ts
file in your e.g. src folder.
// faker.d.ts
declare module '@faker-js/faker' {
import faker from 'faker';
export default faker;
}
An in-depth overview of the API methods is available in the documentation. The API covers the following modules:
Module | Example | Output |
---|---|---|
Address | faker.address.city() | Lake Raoulfort |
Animal | faker.animal.type() | Dog, cat, snake, bear, lion, etc. |
Commerce | faker.commerce.product() | Polo t-shirt |
Company | faker.company.companyName() | Zboncak and Sons |
Database | faker.database.engine() | MyISAM |
Datatype | faker.datatype.uuid() | 7b16dd12-935e-4acc-8381-b1e457bf0176 |
Date | faker.date.past() | Sat Oct 20 2018 04:19:38 GMT-0700 (Pacific Daylight Time) |
Finance | faker.finance.amount() | ¥23400 (After setting locale) |
Git | faker.git.commitMessage() | feat: add products list page |
Hacker | faker.hacker.phrase() | Try to reboot the SQL bus, maybe it will bypass the virtual application! |
Helpers | faker.helpers.userCard() | { avatar: '...', email: '{ first }{ last }{ number }@{domain}', first: '...' } All of the values are self-consistent (e.g. same first + last name in the email, too) |
Image | faker.image.avatar() | https://cloudflare-ipfs.com/ipfs/Qmd3W5DuhgHirLHGVixi6V76LhCkZUz6pnFt5AJBiyvHye/avatar/233.jpg |
Internet | faker.internet.color() | #630c7b |
Lorem | faker.lorem.paragraph() | Word, words, sentences, slug (lorem-ipsum), paragraph(s), text, lines |
Music | faker.music.genre() | R&B |
Name | faker.name.firstName() | Cameron |
Phone | faker.phone.phoneNumber() | +1 291-299-0192 |
Random | faker.random.locale() | fr_CA |
System | faker.system.directoryPath() | C:\Documents\Newsletters\ |
Vehicle | faker.vehicle.vehicle() | 2011 Dodge Caravan |
Faker contains a super useful generator method faker.fake
for combining faker API methods using a mustache string format.
Example:
console.log(
faker.fake('{{name.lastName}}, {{name.firstName}} {{name.suffix}}')
);
This will interpolate the format string with the value of methods name.lastName()
, name.firstName()
, and name.suffix()
Faker has support for multiple locales.
The default language locale is set to English.
Setting a new locale is simple:
// sets locale to de
faker.locale = 'de';
See our documentation for a list of provided languages
Faker supports incremental loading of locales.
// loads only de locale
const faker = require('@faker-js/faker/locale/de');
If you want consistent results, you can set your own seed:
faker.seed(123);
const firstRandom = faker.datatype.number();
// Setting the seed again resets the sequence.
faker.seed(123);
const secondRandom = faker.datatype.number();
console.log(firstRandom === secondRandom);
The project is being built by esbuild (see bundle.ts)
pnpm install
pnpm run build
pnpm install
pnpm run build
pnpm run test
# or
pnpm run coverage
You can view a code coverage report generated in coverage/index.html
.
# build the Faker dist
# it's used inside of certain routes
pnpm run build
pnpm run docs:dev
# build the Faker dist
# it's used inside of certain routes
pnpm run build
pnpm run docs:build # Output docs to /dist
pnpm run docs:serve # Serve docs from /dist
The website is kindly hosted for free by the Netlify team under their Open Source plan. See the netlify.toml for configuration.
Read the team update (January 14th, 2022).
FAQs
Generate massive amounts of fake contextual data
The npm package @faker-js/faker receives a total of 4,406,834 weekly downloads. As such, @faker-js/faker popularity was classified as popular.
We found that @faker-js/faker demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
A threat actor's playbook for exploiting the npm ecosystem was exposed on the dark web, detailing how to build a blockchain-powered botnet.
Security News
NVD’s backlog surpasses 20,000 CVEs as analysis slows and NIST announces new system updates to address ongoing delays.
Security News
Research
A malicious npm package disguised as a WhatsApp client is exploiting authentication flows with a remote kill switch to exfiltrate data and destroy files.