Right now I have:
"faker": "^5.5.3",
"@types/faker": "^5.5.3",
in my packages.json. I'm using 5.5.3 because there's another dependency in the project (codecept) that's locked to the older version.
The original faker project at https://www.npmjs.com/package/faker seems to have been abandoned. It has a joke version number, no description, etc.
I'd like to use the project that is actively maintained at https://www.npmjs.com/package/@faker-js/faker
But when I try:
- in my packages.json:
"@faker-js/faker": "^5.5.3" - importing it with:
import { faker } from '@faker-js/faker';(per faker-js documentation) - call it like
faker.datatype.number(100);
I get:
Cannot read properties of undefined (reading 'datatype')
TypeError: Cannot read properties of undefined (reading 'datatype')
Weirdly, this same code works on a coworker's laptop.
What am I doing wrong? I've tried doing stuff like blowing away node_modules and starting fresh, and running npm install, but no luck.
Changing the import to
import faker from '@faker-js/faker'should solve the problem since faker 5.5.3 uses default imports.You will however get a compiler error, since this version of faker does not provide its own types. There is also no
@typesfor this package and the types in@types/fakerdo not seem to match the@faker-js/fakerpackage. So you can doconst faker = require('@faker-js/faker)and continue without having types. But I would recommend to upgrage the@faker-js/faker'since the newer versions have TypeScript support out of the box.