Flow: Throws error Cannot resolve module "react-redux" even tho it's installed
How to fix it
You have two options:
- stub the dependency by hand
- 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 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
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.
I had the same issue as you.
I resolved it by using flow-typed
I did the following:
- Install
flow-typed
globally. 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-redux
You should see this if the install was successful. - Then try running flow again
$ npm run flow
in your project. The error withreact-redux
will no longer be there.
Alternative solution (for some cases)
Check your .flowconfig
and remove <PROJECT_ROOT>/node_modules/.*
under the field [ignore]
(in case you have it there).
Thanks to @meloseven who solved it here.