Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
245 views
in Technique[技术] by (71.8m points)

javascript - Flow: Throws error Cannot resolve module "react-redux" even tho it's installed

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.

question from:https://stackoverflow.com/questions/51563112/flow-throws-error-cannot-resolve-module-react-redux-even-tho-its-installed

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...