I've followed https://flow.org/en/docs/install/, and flow is working fine when used in single files, like this:
// @flow
type NumberAlias = number;
const n: NumberAlias = "123";
Flow will correctly point out that:
5: const n: NumberAlias = "123";
^^^^^ string. This type is incompatible with
5: const n: NumberAlias = "123";
^^^^^^^^^^^ number
The problem arises when I try to export a type from moduleA and import that type in moduleB:
(moduleA.js)
// @flow
export type NumberAlias = number;
(moduleB.js)
// @flow
import type { NumberAlias } from './moduleA';
const n: NumberAlias = 123;
Flow complains:
src/moduleB.js:3
3: import type { NumberAlias } from './moduleA';
^^^^^^^^^^^ ./moduleA. Required module not found
Isn't this exactly how it's described in https://flow.org/en/docs/types/modules/?
Folder structure is:
src/
moduleA.js
moduleB.js
.flowconfig
package.json
import type only imports declarations to be used for type annotations and declarations. It always gets fully erased, so there's no remnant of it at runtime. Similarly, export type only provides an export that can be used for type contexts, and is also erased from TypeScript's output.
There are two types of exports: Named and Default.
TypeScript supports export = to model the traditional CommonJS and AMD workflow. The export = syntax specifies a single object that is exported from the module. This can be a class, interface, namespace, function, or enum.
JavaScript projects in Flow are composed of “modules” which are just single files that encapsulate some logic. A module can “export” variables/functions/classes so that other modules can make use of them. Additionally they can “import” variables/functions/classes from other modules.
This worked as you'd expect when I set it up, using the two files provided sitting in a src
folder under my project's root. moduleB.js
:
If I fail to put // @flow
in Module A
, the compiler treats NumberAlias
as any
, and fails to complain. I initially forgot to add it, and this threw me for a few minutes.
The only way I could find to produce the "moduleA not found" error was to change the file extension (or file name) for moduleA. For example, if the file is called moduleA.ts
, ModuleA.js
, or simply moduleA
, the import will fail to resolve as above.
Incidentally, both
import type { NumberAlias } from './moduleA';
and
import type { NumberAlias } from './moduleA.js';
resolve just fine in my local environment. You might try the second format?
Provided these conditions are met:
.js
(or .jsx
) extension // @flow
commentThe code you provided works using Flow 0.57.3 with Node 8.7.0. In light of the error message, I'd suggest triple checking the filenames and extensions especially.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With