Using faker in typescript

·

1 min read

Just spent an hour on it - here's how to.

npm i faker@5.5.3
npm i -D @types/faker
  • I install version 5.5.3 - since the latest version at the time of this article is 6.6.6, and it doesn't work (kind of a scary name)

When I used version 6.6.6 I got: find module '...faker\index.js'. Please verify that the package.json has a valid "main" entry

From here it was simple:

import { name, internet } from 'faker';
import { Remult } from "remult";
import { AccountManager } from "../AccountManagers/AccountManager.entity";
import { name, internet } from 'faker';

export async function generateDataIfEmpty(remult: Remult) {
    console.log("here");
    const repo = remult.repo(AccountManager);
    if (await repo.count() == 0) {
        for (let index = 0; index < 10; index++) {
            const firstName = name.firstName();
            const lastName = name.lastName();
            await repo.save({
                firstName,
                lastName,
                email: internet.email(firstName, lastName)
            }, true)
        }
    }
}

PS Apparently there is great drama around this version of the repo, see: What happened to faker.js?