Flow: Does Flow support module path aliasing?

Created on 22 Nov 2014  路  10Comments  路  Source: facebook/flow

From some of the presentations I've seen it seems flow has no issue with commonjs require syntax.

However, Is it possible to feed alias information into flow?

In grunt/gulp I prefer to go the extra mile and feed into the browserify/watchify/wathever task path alias information, so that instead of wriging require('../../../../../../lib/merge') and contextual variation thereof, I can just write require('lib/merge') (or require('subprogram/lib/merge') if the projects more complex) which is much easier to read, reason and grep/refactor. I also usually make directory names point to directory/path/name/index.node files (so I don't have to manually include every little piece of a component, ie. View, Model, etc).

_If flow can only work with dumb require('../../../../lib/merge') calls that's quite the deal breaker._

question

Most helpful comment

nevermind, I found this https://github.com/cdebotton/react-universal/blob/master/.flowconfig

module.name_mapper='^common\/(.*)$' -> '/../common/\1'

I also add to add ../common in [include] because it's beyond my project.

All 10 comments

Tried to workaround this by creating a symlink in node_modules to an actual folder.

Flow's output has changed from
alias/innerFolder/componentName Required module not found
to
componentName Required module not found

but still does not work.

Seems like a deal breaker for me too.

@kompot can you test if it works with the hack of having your source tree folder called node_modules

ie.

/project
    node_modules/       # 3rd party
    src/
        node_modules/   # your stuff
            lib/        # your module
            system/     # your module

_The hack here essentially allows you to reference lib, system (or combinations like lib/whatever) from anywhere in your own modules._

@srcspider yep, it does work, thanks

But the next fail is on require('img/picture.jpg') which is handled by webpack.
I'd be happy to ignore that but could find a way to do that.

@kompot that is indeed bad

Thank you for reporting this issue and appreciate your patience. We've notified the core team for an update on this issue. We're looking for a response within the next 30 days or the issue may be closed.

I think you can probably do this now via the module.name_mapper config option. please reopen if it doesn't work!

I might be missing something, but I can't see how the name_mapper option would help with this. It seems that module.system.node.resolve_dirname could help though.

any chance to solve it without a symlink hack?
I can't make resolve_dirname to work .

nevermind, I found this https://github.com/cdebotton/react-universal/blob/master/.flowconfig

module.name_mapper='^common\/(.*)$' -> '/../common/\1'

I also add to add ../common in [include] because it's beyond my project.

gre's solution worked for me as well, but I had to escape the parentheses:

[include]
../common

[options]
module.name_mapper='^common/\(.*\)$' -> '<PROJECT_ROOT>/../common/\1'
Was this page helpful?
0 / 5 - 0 ratings