Installation
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
Browser
<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 randomCard = faker.helpers.createCard(); // random contact card containing many properties
</script>
Node.js
const faker = require('@faker-js/faker');
const randomName = faker.name.findName(); // Rowan Nikolaus
const randomEmail = faker.internet.email(); // Kassandra.Haley@erich.biz
const randomCard = faker.helpers.createCard(); // random contact card containing many properties
Typescript Support
Types are available via the @types/faker
package. You must manually link them using a *.d.ts
file, like so:
// faker.d.ts
declare module '@faker-js/faker' {
import faker from 'faker';
export default faker;
}
API
An in-depth overview of the API methods is available in the documentation. The API covers the following modules:
Localization
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';
List of locales: az, ar, cz, de, de_AT, de_CH, en, en_AU, en_AU_ocker, en_BORK, en_CA, en_GB, en_IE, en_IND, en_US, en_ZA, es, es_MX, fa, fi, fr, fr_CA, fr_CH, ge, hy, hr, id_ID, it, ja, ko, nb_NO, ne, nl, nl_BE, pl, pt_BR, pt_PT, ro, ru, sk, sv, tr, uk, vi, zh_CN, zh_TW
Individual Localization Packages
Faker supports incremental loading of locales.
// loads only de locale
const faker = require('@faker-js/faker/locale/de');
Setting a randomness seed
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);
Contributing
Building Faker
Faker uses gulp to automate its build process. Each build operation is a separate task which can be run independently.
Browser Bundle
npm run browser
Testing
npm install
npm run test
You can view a code coverage report generated in coverage/lcov-report/index.html.
Developing the docs
# build the Faker library for the browser
# it's used inside of certain routes
npm run browser
npm run docs:dev
Building and serving the docs statically
# build the Faker library for the browser
# it's used inside of certain routes
npm run browser
npm run docs:build # Output docs to /dist
npm run docs:serve # Serve docs from /dist
Deploying Documentation
The website is kindly hosted for free by the Netlify team under their Open Source plan. See the netlify.toml for configuration.
Building JSDocs
JSDOC v3 HTML API documentation
npm run jsdoc
What happened to the original faker.js?
Read the team update (January 14th, 2022).