Ignoring node_modules when using flow

Dan picture Dan · Mar 21, 2017 · Viewed 8.5k times · Source

I am using flow within my React Native application, the initial startup of flow is incredibly slow because it is traversing through my node_modules directory. Flow is reporting masses of errors coming from these third party libraries, which I am unable to fix.

Can I tell flow to ignore any errors within node_modules? I have this in my config file:

[ignore]
.*/node_modules/.*

However, flow now tells throws react-native Required module not found. Weirdly enough, this error isn't picked up when importing React.

Answer

AHM picture AHM · Mar 21, 2017

You can create a folder in your projects root called flow-typed and create declaration files for your modules in there. So in this case you would create the file:

flow-typed/react-native.js

// @flow

declare module 'react-native' {
    /* declarations go here... */
}

The reason that React doesn't complain when included is that flow comes with declarations out of the box (https://github.com/facebook/flow/blob/master/lib/react.js) the same way it comes with declarations for the javascript and browser standard libraries.

Word of warning, though, it is not exactly trivial to write these declaration files. For inspiration look at the declarations Flow comes with, and the ones in the https://github.com/flowtype/flow-typed repository.