My projectâs .flowconfig ignores a few folders, including core/bundles/:
~~~ini
[ignore]
~~~
I am ignoring these files because because they will never have types in them, and I think ignoring them will increase the speed of Flowâs type-checking by telling Flow that it doesnât need to watch them.
I recently added an import within a Flow-typed file of a file within this ignored directory:
~~~javascript
// @flow
import {LRUMap} from '../../../../bundles/lru_map';
// the path resolves to core/bundles/lru_map
// ⊠some code that uses LRUMap âŠ
~~~
I am sure that the relative path '../../../../bundles/lru_map' is correct. The file can run and use LRUMap without errors, and the ESLint rule import/no-unresolved doesnât give any warnings about that path, while it does if I remove or add any ../.
Flow gives me a âRequired module not foundâ error:
~~~
core/tests/scripts/utils/caching/TestLRUMap.js:5
5: import {LRUMap} from '../../../../bundles/lru_map';
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ../../../../bundles/lru_map. Required module not found
Found 1 error
~~~
This is confusing because there really is a module at that path. It was hard to figure out why Flow couldnât find it.
I expect one of two things to happen instead of Flow showing this error:
Flow should report no error for this line, because it can see that the file does exist. Of course, since the imported file has no type annotations, it wonât put any type constraints on how I use LRUMap, but Iâm okay with that.
This solution seems ideal to me; I donât see any reason I shouldnât be able to tell Flow to not watch a file while also importing it within Flow-typed code. And the docs for .flowconfig [ignore] say this is to determine âwhich files to read and watch for changesâ, not to list files that Flow should not be able to see at all.
Flow could report a more specific error for this case with a less confusing message. The message could be something like âCanât import module that is configured to be ignoredâ. Ideally the message would also explain why this is the case and what to do instead, either in the message or by linking to a documentation page.
Currently my workaround is to add a $FlowFixMe suppress_comment above the import line:
~javascript
// $FlowFixMe
import {LRUMap} from '../../../../bundles/lru_map';
~
The authors of these issues and questions encountered this problem too:
You should be using module_mapper in addition and giving all your ignored modules the any type.
@nmn Are you talking about module.name_mapper? I think I see how I could use it as an alternative workaround, by writing UntypedModule.js that contains let value: any; export default value;, and then using module.name_mapper to rewrite all imports within my bundles/ to that same file. Thanks for pointing that out.
But it's still a workaround, and Flow should improve what it does in situations like this. At the very least, the error message shouldn't imply that the imported module doesn't exist (as I described in option 2).
FWIW, this can also happen if you forget to include a sibling or parent folder in the [include] section of your config, and try to import something from outside of your project root.
There is [untyped] section which takes list of regexp similar to [ignore] but treat those modules as any.
This probably can be closed.
/cc @vkurchatkin
Most helpful comment
@nmn Are you talking about
module.name_mapper? I think I see how I could use it as an alternative workaround, by writingUntypedModule.jsthat containslet value: any; export default value;, and then usingmodule.name_mapperto rewrite all imports within mybundles/to that same file. Thanks for pointing that out.But it's still a workaround, and Flow should improve what it does in situations like this. At the very least, the error message shouldn't imply that the imported module doesn't exist (as I described in option 2).