Flow: How to import directories from project root without relative paths?

Created on 21 May 2016  路  2Comments  路  Source: facebook/flow

I have a folder called actions at the root of my project and when I import it using an absolute path, flow complains that it cannot find the folder. But when I use relative paths, it works. Is there something I'm doing wrong?

import ax from 'actions' // Doesn't work
import ax from '../actions'  /// Works

Most helpful comment

By default, Flow uses the same module resolution algorithm as Node -- so in that scheme, if there is an import that's not relative, the rule is to walk up the directory path and look in a folder called node_modules for a package by that name.

If you have a custom module system of some kind that doesn't work this way, one thing you can do is use the module.name_mapper config option in your .flowconfig file to map tell Flow what it means to do import ax from 'actions';:

module.name_mapper= '^actions\(.*\)$' -> '<PROJECT_ROOT>/\1'

Note that name_mappers are just regex search/replaces that Flow applies right before it tries to find a given module for an import.

Hope that helps! Feel free to re-open if you still have issues.

All 2 comments

By default, Flow uses the same module resolution algorithm as Node -- so in that scheme, if there is an import that's not relative, the rule is to walk up the directory path and look in a folder called node_modules for a package by that name.

If you have a custom module system of some kind that doesn't work this way, one thing you can do is use the module.name_mapper config option in your .flowconfig file to map tell Flow what it means to do import ax from 'actions';:

module.name_mapper= '^actions\(.*\)$' -> '<PROJECT_ROOT>/\1'

Note that name_mappers are just regex search/replaces that Flow applies right before it tries to find a given module for an import.

Hope that helps! Feel free to re-open if you still have issues.

The syntax seems _very_ picky in .flowconfig. Your example failed for me (flow threw a parsing error). But I changed it to this:

module.name_mapper='^actions\(.*\)$' -> '<PROJECT_ROOT>/\1'

And now it works. Or in my case (support for root level imports):

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