Search code examples
flowtypeflow-typed

Flow takes very long to start up because it checks node_modules


I added Flow to my React/Redux/Webpack project and initially, it was great, I loved the type annotations. Over time, I noticed that the start up time of the project became really slow - around 120s; it used to be around 20s. Upon investigation, I realized that it was because Flow was scanning through all the JS files in node_modules.

I attempted to ignore node_modules in .flowconfig by adding:

[ignore]
.*node_modules/.*

The start up became fast again but Flow would complain Required module not found in places where I import external libraries from my code.

A workaround suggested in this Github issue was to flowignore the node_modules and manually add interfaces for external libraries. This seems to work but is a hassle to maintain when new libraries are added into the project.

It's frustrating to have to wait almost 2 minutes each time I start the project, are there any better ideas?


Solution

  • One way to avoid the Required module not found errors would be to run flow-typed install which will fetch existing libdefs for popular libraries from the flow-typed repository. It also generates stubs for the libraries that cannot be found in it. This works great for many projects but in some rare cases, the stubs for certain libraries like Immutable.js were not generated.

    I eventually came up with cli command flow-scripts to automatically generate the libdef stub interfaces such that I could flowignore node_modules but not get the Required module not found errors.

    Simply run

    $ flow-scripts stub
    

    in the root of the repository and the libdef stubs will be automatically generated in flow-typed directory.