22

As in topic. I cannot handle it :/ I've installed npm i faker and also npm i @types/faker --save-dev

my IDE found fakerStatic.name.findName() but browser not recognizes fakerStatic.

import 'faker'; does not help.

piernik
  • 3,507
  • 3
  • 42
  • 84

2 Answers2

55

You have to import it like:

import * as faker from 'faker';

And use it like:

faker.name.findName()
Saravana
  • 37,852
  • 18
  • 100
  • 108
  • 3
    Works but got another problem `faker.locale = 'pl';` gives error `Cannot assign to 'locale' because it is a constant or a read-only property.` – piernik Jul 24 '17 at 11:23
  • 2
    Not sure what the issue is. [`locale` is not defined as read-only/const in the definition files.](https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/faker/index.d.ts#L10). Try posting a new question? – Saravana Jul 24 '17 at 16:34
  • @piernik Did you solve that issue? I'm getting the same message despite it not being defined as read-only/const. –  Oct 30 '17 at 12:44
  • 6
    @AndyJ @piernik you specify the locale in the import. Try `import * as faker from "faker/locale/pl";` – Michael Schnerring Nov 13 '17 at 15:50
  • 1
    @MichaelSchnerring Thanks for the suggestion. I tried that and get another error. I assume it must be something to do with my development environment. Really, I should post this all as a new question with the relevant info as Saravana suggested! –  Nov 13 '17 at 16:20
  • @AndyJ for me all I needed to do was importing it like that. Setting `faker.locale` is not required. – Michael Schnerring Nov 13 '17 at 17:24
  • For me `import * as faker from 'Faker';` was required. – Arun Jan 04 '18 at 17:17
  • @AndyJ Did you figure this out? I'm getting the same problem: https://stackoverflow.com/q/51642159/65387 – mpen Aug 01 '18 at 21:25
  • 2
    Hello, I added faker to my `devDependencies` in `package.json`. Did the import as mentioned and um using `const newUser = _.set(_.cloneDeep(user), 'key', faker.phone.phoneNumber);` but getting an error *Cannot find module 'faker'* – Mona101ma Aug 14 '21 at 19:02
3

The documentation now says:

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

If it is to be localized data, the desired locale can also be attached.

import { faker } from '@faker-js/faker/locale/de'
Michael S.
  • 589
  • 8
  • 25