[英]cannot find module faker after npm install --save-dev
I want to install all my modules locally so I am installing everything with the "--save-dev" switch which updates the package.json.我想在本地安装所有模块,所以我使用“--save-dev”开关安装所有模块,该开关更新 package.json。
I am trying to include this module so I installed using this command:我正在尝试包含此模块,因此我使用以下命令进行了安装:
npm install Faker --save-dev
My app structure is like this:我的应用结构是这样的:
app controllers models node_modules Faker server.js应用控制器模型 node_modules Faker server.js
So Faker is in the right place but when I add this code in my server.js file:所以 Faker 是在正确的地方,但是当我在 server.js 文件中添加这段代码时:
var faker = require('./Faker');
I get the following error message:我收到以下错误消息:
Error: Cannot find module './Faker'
at Function.Module._resolveFilename (module.js:338:15)
at Function.Module._load (module.js:280:25)
at Module.require (module.js:364:17)
at require (module.js:380:17)
at Object.<anonymous> (/Users/paulcowan/projects/async-talk/server.js:23:13)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
But this works:但这有效:
var Faker = require('./node_modules/Faker');
I did not think that I would have to include the node_modules part.我不认为我必须包括 node_modules 部分。
After a yarn upgrade --latest
I got this error for another reason: A 6.6.6
version has been published 13 days ago and it's totally empty ( only one commit called endgame
): https://www.npmjs.com/package/faker .在yarn upgrade --latest
我收到此错误的另一个原因是:13 天前发布了6.6.6
版本,它完全是空的( 只有一个名为endgame
的提交): https ://www.npmjs.com/package/伪造者。
Is it a hack?是黑客吗? Is it a joke?是笑话吗? If yes, is it fun?如果是的话,好玩吗? Not sure...没有把握...
To fix it simply rollback to the last version.要修复它,只需回滚到上一个版本。
yarn add --dev faker@5.5.3
# or if you use npm
npm install --save-dev faker@5.5.3
Following the answer of akop
below, the safest way seems to switch to faker-js
按照下面akop
的回答,最安全的方法似乎是切换到faker-js
yarn remove faker
yarn add --dev @faker-js/faker
Then change your imports然后改变你的进口
import * as faker from 'faker';
// to
import * as faker from '@faker-js/faker';
There is also a type definition for Typescript on their doc but it worked without for me: github.com/faker-js/faker他们的文档上还有一个 Typescript 的类型定义,但它对我来说没有: github.com/faker-js/faker
Faker (and colors ) was destroyed by the author. Faker (和colors )被作者销毁了。 The reason is not totally clear. 原因并不完全清楚。
You can use the version before the endgame-commit (5.5.3), as it already Jean Claveau described .您可以使用 endgame-commit (5.5.3) 之前的版本,正如Jean Claveau 所描述的那样。
For the future, you should switch to the new community version, it is called fake-js .对于未来,你应该切换到新的社区版本,它被称为fake-js 。 Have a look here: Github of new faker-js看看这里:新 faker-js 的 Github
There is also a faq, which also explains what happens to the old version.还有一个常见问题解答,它也解释了旧版本会发生什么。
I want to mention everyone why this problem happened.我想提一下大家为什么会出现这个问题。
The owner of faker deleted it from GitHub at v6.6.6 faker 的所有者在 v6.6.6 从 GitHub 删除了它
You can see that here:你可以在这里看到:
https://github.com/marak/Faker.js/ https://github.com/marak/Faker.js/
So it is up to the community to complete it所以由社区来完成它
The new repository:新的存储库:
https://github.com/faker-js/faker https://github.com/faker-js/faker
This is the official, stable fork of Faker.这是 Faker 的官方稳定分支。
Please replace your faker
dependency with @faker-js/faker
.请用@faker-js/faker
替换你的faker
依赖项。
npm install @faker-js/faker --save-dev
const { faker } = require('@faker-js/faker');
Typescript users coming by, remember to install @types/faker
.过来的打字稿用户,记得安装@types/faker
。 Then, import faker from 'faker';
然后, import faker from 'faker';
works without errors.工作没有错误。
To get your require
to work, you need do:要使您的require
正常工作,您需要执行以下操作:
var Faker = require('Faker');
Any package installed by npm is required by name. npm 安装的任何包都需要按名称。 Only modules that are required locally need a path-like require.只有本地需要的模块才需要类似路径的要求。 Your require(./Faker);
你的require(./Faker);
means "require a module from the same directory as this file, called 'Faker'" .表示“需要与此文件位于同一目录中的模块,称为'Faker'” 。
Remove the ./
.删除./
。 You're telling Node to look for the module in the current directory.您是在告诉 Node 在当前目录中查找模块。
var faker = require('Faker');
The latest version of faker is v7.3.0 . faker 的最新版本是 v7.3.0 。
You should import faker in this way:您应该以这种方式导入faker:
import { faker } from '@faker-js/faker';
This one below doesn't work:下面这个不起作用:
import faker from 'faker';
Genuine solution: Roll back to the previous version of the faker ie install the 5.5.3 or any lower version.正版解决方案:回滚到之前的faker版本,即安装5.5.3或更低版本。 It will work fine.它会正常工作。
npm install faker @5.5.3 npm 安装faker@5.5.3
or或者
yarn install faker @5.5.3纱线安装 faker @5.5.3
NOTE: Faker has been changed to fakerjs so if you want to use latest version of faker please install it from fakerjs using the following command:~注意:Faker 已更改为 fakerjs,因此如果您想使用最新版本的 faker,请使用以下命令从 fakerjs 安装它:~
npm i @faker-js/faker npm 我@faker-js/faker
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.