[英]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.在我的 packages.json 中。 I'm using 5.5.3 because there's another dependency in the project (codecept) that's locked to the older version.我正在使用 5.5.3,因为项目 (codecept) 中有另一个依赖项被锁定到旧版本。
The original faker project at https://www.npmjs.com/package/faker seems to have been abandoned.原来在https://www.npmjs.com/package/faker的 faker 项目好像已经废弃了。 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我想使用在https://www.npmjs.com/package/@faker-js/faker积极维护的项目
But when I try:但是当我尝试时:
"@faker-js/faker": "^5.5.3"
在我的 packages.json: "@faker-js/faker": "^5.5.3"
import { faker } from '@faker-js/faker';
导入它: import { faker } from '@faker-js/faker';
(per faker-js documentation) (根据 faker-js 文档)faker.datatype.number(100);
称它为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.我试过做一些事情,比如吹走node_modules
并重新开始,然后运行npm install
,但没有成功。
Changing the import to import faker from '@faker-js/faker'
should solve the problem since faker 5.5.3 uses default imports.将导入更改为import faker from '@faker-js/faker'
应该可以解决问题,因为 faker 5.5.3 使用默认导入。
You will however get a compiler error, since this version of faker does not provide its own types.但是你会得到一个编译器错误,因为这个版本的 faker 没有提供它自己的类型。 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.这个 package 也没有@types
并且@types/faker
中的类型似乎与@faker-js/faker
package 不匹配。所以你可以做const faker = require('@faker-js/faker)
并继续没有类型。 But I would recommend to upgrage the @faker-js/faker'
since the newer versions have TypeScript support out of the box.但我建议升级@faker-js/faker'
因为新版本开箱即用地支持 TypeScript。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.