代码之家  ›  专栏  ›  技术社区  ›  Kunok

Flow:Throws error无法解析模块“react redux”,即使它已安装

  •  17
  • Kunok  · 技术社区  · 6 年前

    即使THO模块被安装并存在,流也不能解决它并抛出错误。 见下文: (1)我跑的内线球 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

    这很可能是流的bug,我也提交了问题。

    4 回复  |  直到 6 年前
        1
  •  40
  •   cogell MikhailSP    6 年前

    如何修复

    你有两个选择:

    1. 手工存根依赖关系
    2. 引进 flow-typed 查找依赖项类型 为您归档/存根

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

    选择1

    .flowconfig ,在下面添加目录 [libs] ,

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

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

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

    选择2

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

    为什么会发生这种错误

    Flow正在寻找 类型 正在导入的模块的定义。因此,当模块确实存在时 /node_modules 该模块的代码中没有签入类型定义文件。

        2
  •  4
  •   zulucoda    6 年前

    我和你有同样的问题。

    enter image description here

    我用 flow-typed

    我做了以下工作:

    1. 安装 流类型 全球地。例子: $ npm install -g flow-typed
    2. 然后在项目根文件夹中运行 $ flow-typed install react-redux@5.0.x 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 flow 在你的项目中。错误 react-redux 将不再存在。
        3
  •  0
  •   exmaxx    6 年前

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

    检查您的 .flowconfig 并移除 <PROJECT_ROOT>/node_modules/.* 在田野下面 [ignore] (以防万一)。


    多亏了解决这个问题的@meloseven here .

        4
  •  0
  •   Ivan_ug    6 年前

    我查了一下 package.json 存档并通知 react-redux 不见了。我手动将其添加到依赖项中 "react-redux": "x.x.x" 然后跑 npm install 之后。请注意,版本号应与其他模块兼容。

        5
  •  0
  •   Uncaught Exception    5 年前

    请确保在.flowconfig的“ignore”下提供路径,如下所示:

    [ignore]
    .*/node_modules/react-native/Libraries/.* 
    

    不是这样的:

    .*/node_modules/react-native/Libraries/Components
    .*/node_modules/react-native/Libraries/Core
    ....