Flow: Setting content root causes modules to be not found

Created on 25 Oct 2019  路  8Comments  路  Source: facebook/flow

Flow version: 0.110.0 ->

Expected behavior

Flow should understand path patterns like previous versions does.

Actual behavior

Flow says

"Cannot resolve module `js/test2`."

It's quite common pattern to reduce complexity of imports by setting project root as absolute root so that it can be used in imports. In 0.110.0 -> versions Flow fails these cases.

You can repro the issue by cloning the repo, and opening js2/test.jsfile in editor. Interestingly, yarn flow does not give any errors, which is expected behaviour.

This is probably the original message that sparked such settings: https://github.com/facebook/flow/issues/4186#issuecomment-347078321 and this article has made it more popular: https://medium.com/@sherryhsu/how-to-change-relative-paths-to-absolute-paths-for-imports-32ba6cce18a5

E: create-react-app suggest following: https://create-react-app.dev/docs/adding-flow/ module.name_mapper='^\([^\.].*\)$' -> '<PROJECT_ROOT>/src/\1'

bug feature request flowconfig module resolution

Most helpful comment

I imagine this will trip up a bunch of people. What we did to fix it:

.flowconfig module.system.node.allow_root_relative=true module.system.node.root_relative_dirname=./app

All 8 comments

Thanks a ton for taking the time to put this together!

I took a look at your repro, and I'm seeing the same behavior I noticed before

If I git clone the repro and run flow check I see

Error ------------------------------------------------------------------------------------------------- js2/test.js:3:18

Cannot resolve module `js/test2`.

   3| import test from 'js/test2';
                       ^^^^^^^^^^



Found 1 error

If I run mkdir node_modules, I then see

Found 0 errors

In the IDE, when I open the files I see the same errors


AFAICT, module.system.node.resolve_dirname=. shouldn't work for anyone. The fact that it works here is almost certainly a bug in the module.system.node.resolve_dirname feature. I'm currently not sure why it works.

If you set module.system.node.resolve_dirname=foo, then any directory named foo would be treated as if it were named node_modules.

Setting module.system.node.resolve_dirname=flow-bug for your repository would be closer to what https://github.com/facebook/flow/issues/4186#issuecomment-347078321 originally suggested. It would treat the root of your directory structure as a node_modules directory and allow paths relative to that. It also somehow avoids the error that occurs above when node_modules is deleted.

However, putting your source files inside of a directory which Flow treats as a node_modules directory is probably not ideal. Flow generally considers code within node_modules as third-party code and less interesting than the user's own code. I worry that this hack might trigger that.


So my thoughts are that two things are appropriate here:

  1. Fix module.system.node.resolve_dirname to disable whatever bug was allowing you to use .
  2. Add module.system.node.allow_root_relative=true or something like that to keep your use case supported

@gabelevi experimental.disable_live_non_parse_errors=true does not fully fix the situation. The node modules will not be found after setting the flag :/

Also setting the module.system.node.resolve_dirname=flow-bug (or similar in our main repo) didn't help.

module.name_mapper='^\([^\.].*\)$' -> '<PROJECT_ROOT>/\1' did the trick for setting absolute paths, found from here: https://create-react-app.dev/docs/adding-flow/

Another issue with this is that Flow can't resolve any node modules even with empty flowconfig. flow check runs without errors, but opened file reports Cannot resolve module 'recompose' even when added & installed "recompose": "0.26.0" in package.json. It's not only recompose but any node module that doesn't have libdef files.

@gabelevi experimental.disable_live_non_parse_errors=true does not fully fix the situation. The node modules will not be found after setting the flag :/

That's actually a little reassuring. I was having trouble understanding how live non-parse errors (e.g. live type errors) was interacting with this issue.

And yeah, that name mapper solution would turn any import that doesn't start with a . into an absolute path. That sounds like it would break importing anything from node modules.

Ok! This should be done now!

Nice, thanks a lot @gabelevi!

I imagine this will trip up a bunch of people. What we did to fix it:

.flowconfig module.system.node.allow_root_relative=true module.system.node.root_relative_dirname=./app

Was this page helpful?
0 / 5 - 0 ratings

Related issues

blax picture blax  路  83Comments

cletusw picture cletusw  路  52Comments

Macil picture Macil  路  47Comments

xtinec picture xtinec  路  65Comments

jlongster picture jlongster  路  55Comments