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(); // [email protected]
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(); // [email protected]
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(); // [email protected]
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:
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).