javascript 流程:抛出错误即使安装了模块“react-redux”也无法解析
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/51563112/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Flow: Throws error Cannot resolve module "react-redux" even tho it's installed
提问by Kunok
Even tho module is installed and it exists, Flow cannot resolve it and throws error.
See below:
1) Inside bash I ran flowand it throws error that module is not found
即使安装了模块并且它存在,Flow 也无法解决它并引发错误。见下文:1)在我运行的 bash 中flow,它抛出了找不到模块的错误
user@pc:~/code/project$ flow
Error ┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈ src/functionalities/Growth/index.js:3:25
Cannot resolve module react-redux.
1│ // @flow
2│ import React from "react"
3│ import { connect } from "react-redux"
4│
5│ type Props = {
6│ children: Function
Found 1 error
2) Below command checks whether directory exists and it does
2)下面的命令检查目录是否存在,它确实存在
user@pc:~/code/project$ ls node_modules | grep react-redux
react-redux
I tried to remove and reinstall both node_modulesdirectory and yarn.lockfile.
我试图删除并重新安装node_modules目录和yarn.lock文件。
Versions should be matching:
版本应该匹配:
flow version
Flow, a static type checker for JavaScript, version 0.77.0
.flowconfig:
.flowconfig:
[version]
0.77.0
This is very likely bug with Flow, I also submitted issue.
这很可能是 Flow 的错误,我也提交了问题。
回答by cogell
How to fix it
如何修复
You have two options:
您有两个选择:
- stub the dependency by hand
- bring in
flow-typedto find the dependency type file/stub it for you
- 手动存根依赖
- 引入
flow-typed为您查找依赖类型文件/存根
I use option 2 but it is nice to know what is happening underneath
我使用选项 2 但很高兴知道下面发生了什么
Option 1
选项1
In .flowconfig, add a directory under [libs],
在.flowconfig, 下添加一个目录[libs],
...
[libs]
/type-def-libs
...
Now, create that directory at your project root and a file /type-def-libs/react-reduxwhich contains,
现在,在您的项目根目录和一个/type-def-libs/react-redux包含以下内容的文件中创建该目录,
declare module 'react-redux' {
declare module.exports: any;
}
Option 2
选项 2
- install
flow-typed, if using yarnyarn add -D flow-typed- I prefer to install every locally to the project when possible
- run
yarn flow-typed install- this will install any type definition files for modules that it finds AND it will stub any modules it doesn't find, which is similar to what we did in option 1
- 安装
flow-typed,如果使用纱线yarn add -D flow-typed- 如果可能,我更喜欢将每个本地安装到项目中
- 跑
yarn flow-typed install- 这将为它找到的模块安装任何类型定义文件,并且它将存根它没有找到的任何模块,这类似于我们在选项 1 中所做的
Why is this error happening
为什么会发生这个错误
Flow is looking for the typedefinition for the module you are importing. So while the module does exist in /node_modulesthat module doesn't have a type definition file checked into its code.
Flow 正在寻找您正在导入的模块的类型定义。因此,虽然该模块确实存在于/node_modules该模块中,但没有将类型定义文件签入其代码。
回答by zulucoda
I had the same issue as you.
我和你有同样的问题。
I resolved it by using flow-typed
我通过使用解决了它 flow-typed
I did the following:
我做了以下事情:
- Install
flow-typedglobally. example:$ npm install -g flow-typed - Then inside your project root folder, run
$ flow-typed install [email protected]
? Searching for 1 libdefs... ? flow-typed cache not found, fetching from GitHub... ? Installing 1 libDefs... ? react-redux_v5.x.x.js └> ./flow-typed/npm/react-redux_v5.x.x.js react-reduxYou should see this if the install was successful. - Then try running flow again
$ npm run flowin your project. The error withreact-reduxwill no longer be there.
flow-typed全局安装。例子:$ npm install -g flow-typed- 然后在您的项目根文件夹中,运行
如果安装成功,您应该会看到这一点。
$ flow-typed install [email protected]
? Searching for 1 libdefs... ? flow-typed cache not found, fetching from GitHub... ? Installing 1 libDefs... ? react-redux_v5.x.x.js └> ./flow-typed/npm/react-redux_v5.x.x.js react-redux - 然后尝试
$ npm run flow在您的项目中再次运行 flow 。错误react-redux将不再存在。
回答by exmaxx
Alternative solution (for some cases)
替代解决方案(对于某些情况)
Check your .flowconfigand remove <PROJECT_ROOT>/node_modules/.*under the field [ignore](in case you have it there).
检查您的.flowconfig并<PROJECT_ROOT>/node_modules/.*在该字段[ignore]下删除(如果您在那里有它)。
Thanks to @meloseven who solved it here.
回答by Ivan_ug
I checked my package.jsonfile and noticed react-reduxwas missing. I manually added it to the dependencies "react-redux": "x.x.x"and ran npm installthereafter. Note that the version number should be compatible with the other modules.
我检查了我的package.json文件,发现react-redux丢失了。我手动将它添加到依赖项中"react-redux": "x.x.x",npm install然后运行。请注意,版本号应与其他模块兼容。
回答by Uncaught Exception
Please ensure that you provide the path under 'ignore' in .flowconfig, like this:
请确保您在 .flowconfig 中的“忽略”下提供路径,如下所示:
[ignore]
.*/node_modules/react-native/Libraries/.*
and not like this:
而不是这样:
.*/node_modules/react-native/Libraries/Components
.*/node_modules/react-native/Libraries/Core
....

