#1118·faker

Export individual API modules on their own

Author: Cephyric-ghCreated Jun 29, 2022Updated Sep 3, 2026
Labelsc: featurep: 1-normals: accepted

Clear and concise description of the problem

When using the package, even if you only ever needed 1 specific function you have to import the entire thing into your project in order to use it, which leaves it at the mercy of tree-shaking (assuming you're bundling the project that makes use of faker, which I'd assume is most use-cases).

Suggested solution

Instead of exporting everything under the to-level faker class, instead export each API module separately to allow only importing the things that are actually being used

typescript
// Current way to import, meaning we have to hope that the specific
// bundler being used (rollup, webpack, esbuild, etc) tree-shakes everything else away
import { faker } from '@faker-js/faker';

const noun = faker.word.noun();

vs

typescript
// Proposed solution, guarantees that at most the only thing that is
// bundled in is all the underlying functions for the "word" module
import { word } from '@faker-js/faker';

const noun = word.noun();

Alternative

The APIs could be exported alongside the main export, which would remov th breaking change

typescript
import { faker, word } from '@faker-js/faker';

Or the APIs could be exported in their own nested namespace (the same as locales are, though that may make locales funky)

typescript
import { faker } from '@faker-js/faker';
import { word } from '@faker-js/faker/word';
import { address } from '@faker-js/faker/locale/de/word';

Additional context

No response