37

Even tho module is installed and it exists, Flow cannot resolve it and throws error. See below: 1) Inside bash I ran flow and it throws error that module is not found

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

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

I tried to remove and reinstall both node_modules directory and yarn.lock file.

Versions should be matching:

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

.flowconfig:

[version]
0.77.0

enter image description here

This is very likely bug with Flow, I also submitted issue.

frontendgirl
  • 832
  • 5
  • 13
Kunok
  • 5,543
  • 8
  • 38
  • 72
  • 3
    Meanwhile if someone else comes upon similar issue use `// $FlowFixMe` above import to ignore error. – Kunok Jul 27 '18 at 18:20

5 Answers5

60

How to fix it

You have two options:

  1. stub the dependency by hand
  2. bring in flow-typed to find the dependency type file/stub it for you

I use option 2 but it is nice to know what is happening underneath

Option 1

In .flowconfig, add a directory under [libs],

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

Now, create that directory at your project root and a file /type-def-libs/react-redux which contains,

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

Option 2

  • install flow-typed, if using yarn yarn 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

Why is this error happening

Flow is looking for the type definition for the module you are importing. So while the module does exist in /node_modules that module doesn't have a type definition file checked into its code.

Community
  • 1
  • 1
cogell
  • 2,852
  • 1
  • 18
  • 19
  • Thank you. Is it due to a recent change? This looks like Typescript (forced to type everything) . – MacKentoch Oct 27 '18 at 05:56
  • @MacKentoch The type syntax between Flow and Typescript is very similar. Is this a recent change [to flow]? This is not new in Flow (using over the past 1yr+ as memory serves) you have needed to stub un-typed imports since the beginning of my use. – cogell Oct 29 '18 at 14:13
  • Have you been able to make the option 2 with flow-typed work in Intellij/Webstorm? – amanteaux Nov 16 '18 at 12:55
  • Option 2 worked perfectly for me on webStorm.. thanks – Paulo Griiettner Mar 19 '19 at 00:17
7

I had the same issue as you.

enter image description here

I resolved it by using flow-typed

I did the following:

  1. Install flow-typed globally. example: $ npm install -g flow-typed
  2. Then inside your project root folder, run $ 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 You should see this if the install was successful.
  3. Then try running flow again $ npm run flow in your project. The error with react-redux will no longer be there.
zulucoda
  • 778
  • 10
  • 24
  • 4
    Thank you this helped a lot. I adapted the solution a bit though, here is what I came up with: 1. install `flow-typed` locally. `npm i -D flow-typed` 2. within `package.json` scripts section add `'flow-typed: flow-typed`, 3. from the root of your project run `npm run flow-typed create-stub ` – KaeyangTheG Jan 01 '19 at 00:55
3

Alternative solution (for some cases)

Check your .flowconfig and remove <PROJECT_ROOT>/node_modules/.* under the field [ignore] (in case you have it there).

UPDATE 1 - or you can add !<PROJECT_ROOT>/node_modules/react-redux/.* after <PROJECT_ROOT>/node_modules/.*. this will ignore all the modules except for react-redux


Thanks to @meloseven who solved it here.

Arka
  • 398
  • 1
  • 2
  • 14
exmaxx
  • 2,166
  • 21
  • 23
  • Yes, as I mentioned, it only works for certain cases. See discussion here for more info about when it helped: https://github.com/facebook/flow/issues/6646#issuecomment-447272772 – exmaxx Mar 27 '20 at 17:54
0

I checked my package.json file and noticed react-redux was missing. I manually added it to the dependencies "react-redux": "x.x.x" and ran npm install thereafter. Note that the version number should be compatible with the other modules.

Ivan_ug
  • 1,889
  • 2
  • 12
  • 10
0

Please ensure that you provide the path under 'ignore' in .flowconfig, like this:

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

and not like this:

.*/node_modules/react-native/Libraries/Components
.*/node_modules/react-native/Libraries/Core
....
Uncaught Exception
  • 1,949
  • 14
  • 23