流程:引发错误即使安装它也无法解析模块“react-redux”

问题描述 投票:17回答:4

即使tho模块已安装且存在,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)下面的命令检查目录是否存在,确实存在

user@pc:~/code/project$ ls node_modules | grep react-redux
react-redux

我试图删除并重新安装node_modules目录和yarn.lock文件。

版本应匹配:

flow version
Flow, a static type checker for JavaScript, version 0.77.0

.flowconfig:

[version]
0.77.0

enter image description here

这很可能是Flow的错误,我也提交了问题。

javascript react-redux flowtype
4个回答
34
投票

如何解决它

您有两种选择:

  1. 手动存根依赖
  2. 引入flow-typed为您找到依赖类型文件/存根

我使用选项2,但很高兴知道下面发生了什么

Option 1

.flowconfig中,在[libs]下添加一个目录,

...
[libs]
/type-def-libs
...

现在,在项目根目录和包含的文件/type-def-libs/react-redux中创建该目录,

declare module 'react-redux' {
  declare module.exports: any;
}

Option 2

  • 安装flow-typed,如果使用纱线yarn add -D flow-typed 我希望尽可能在本地安装每个项目
  • 运行yarn flow-typed install 这将为它找到的模块安装任何类型定义文件,它将存根它找不到的任何模块,这与我们在选项1中所做的类似

为什么会发生这种错误

Flow正在查找要导入的模块的类型定义。因此,虽然模块确实存在于/node_modules中,但该模块没有在其代码中检入类型定义文件。


4
投票

我和你有同样的问题。

enter image description here

我用flow-typed解决了这个问题

我做了以下事情:

  1. 全球安装flow-typed。例如:$ npm install -g flow-typed
  2. 然后在项目根文件夹中运行$ flow-typed install [email protected] enter image description here • 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 如果安装成功,您应该看到这个。
  3. 然后尝试在项目中再次运行$ npm run flowreact-redux的错误将不再存在。

0
投票

替代解决方案(对于某些情况)

检查你的.flowconfig并删除字段<PROJECT_ROOT>/node_modules/.*下的[ignore](如果你有它)。


感谢@meloseven解决了它here


0
投票

我检查了我的package.json文件,发现react-redux丢失了。我手动将它添加到依赖项"react-redux": "x.x.x",然后运行npm install。请注意,版本号应与其他模块兼容。

© www.soinside.com 2019 - 2024. All rights reserved.