What is the right way to import Faker JS?

1.4k Views Asked by At

I see that when importing Faker JS into my NodeJS project and then build it, it bundles the whole module inside it. I just want to bundle a specific module inside it. Is it possible?

I, for example, use only the faker.finance.currencyName() function. I tried importing just import "faker/lib/finance" but that doesn't seem to work. The dist bundle is a whopping 824.67kb (when doing npm run build in vite-js).

2

There are 2 best solutions below

2
On

Looking at what is exported, it does not appear that specific sub-modules can be imported.

0
On

@Manny is correct but the links are dead. After the FakerJS move something like this would work (TypeScript syntax)

import { name } from '@faker-js/faker';

const firstName = name.firstName();

or

import { 
  name as fakerName, 
  address as fakerAddress, 
  phone as fakerPhone
} from '@faker-js/faker';

const testUser = {
  firstName: fakerName.firstName(),
  lastName: fakerName.lastName(),
  phoneNumber: fakerPhone.phoneNumber(),
  city: fakerAddress.city()
};