Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React: Flow: Import a local js file - cannot resolve issue module

I am using Flow: Static type checking library for react front end application, it throws "cannot resolve" for internal imports from src directory:

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

EDITED: My .flowconfig looks like this after I installed flow-typed:

[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]

How do I map flow for such imports in .flowconfig file ?

Thanks in advance.

like image 542
Jarvis Avatar asked Oct 01 '18 22:10

Jarvis


2 Answers

Got it working.

Relative paths don't really work with Flow.

We need to map imports from Project root for Flow to understand.

I changed my import to

import words from 'def/words';

Add the following to your .flowconfig file

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

Fixes above error.

like image 117
Jarvis Avatar answered Sep 22 '22 21:09

Jarvis


To get to ./def from ./hello.jsx, you have to go up another directory

import words from '../../words';

like image 28
mhkit Avatar answered Sep 19 '22 21:09

mhkit