我使用Flow: Static type checking library作为反应前端应用程序,它从src目录中抛出“无法解析”内部导入:
Example in file at path: src/abc/def/ghi/hello.jsx, I am using the following import:
import words from '../words';
--> Throws error "Cannot resolve module ../words
words.js is in src/abc/def dir
编辑:我安装了flow-typed后,我的.flowconfig看起来像这样:
[ignore]
.*/node_modules/.*
.*/build/.*
.*/dist/.*
[include]
.*/src/.*
.*/test/.*
[libs]
flow-typed
[lints]
[options]
all=true
module.system.node.resolve_dirname=node_modules
module.system.node.resolve_dirname=src
[strict]
如何在.flowconfig文件中映射此类导入的流量?
提前致谢。
要从./hello.jsx获取./def,您必须进入另一个目录
从'../../words'导入单词;
搞定了。
相对路径不适用于Flow。
我们需要映射Project的根目录,以便Flow了解。
我将导入更改为
import words from 'def/words';
将以下内容添加到.flowconfig文件中
module.name_mapper='^def\(.*\)$' -> '<PROJECT_ROOT>/src/abc/def/\1'
修复上面的错误。
/path/to/project/node_modules/.bin/flow stop
/path/to/project/yarn run flow (or your flow invoking script)
这解决了我的问题。