typescriptnpmundefinedfaker.js

Can't use @faker-js/faker in TypeScript project -- undefined -- but older "faker" import works. Why?


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:

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.


Solution

  • 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 @types for this package and the types in @types/faker do not seem to match the @faker-js/faker package. So you can do const 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.