flow icon indicating copy to clipboard operation
flow copied to clipboard

Using `flow-remove-types` will fail when using ES Modules

Open callumgare opened this issue 4 years ago • 3 comments

If flow-remove-types is run in a repo where package.json contains "type": "module" then it will fail. This occurs with the latest version of flow-remove-types (2.147.0) and flow (0.147.0). I've thrown together an example repo for reproduction: https://github.com/callumgare/example-flow-node-es-modules-fail

When running npx flow-remove-types index.js in the above repo with node 14 under ubuntu I get:

file:///home/callumgare/repos/flow-node-test/index.js:2
function main(val: string) {
                 ^

SyntaxError: Unexpected token ':'
    at Loader.moduleStrategy (internal/modules/esm/translators.js:141:18)
    at async link (internal/modules/esm/module_job.js:42:21)

callumgare avatar Mar 22 '21 01:03 callumgare

I get the same issue when using flow-node to execute files.

I've had to fall back to combining require for values and import for types.

geraintwhite avatar May 28 '21 15:05 geraintwhite

I encountered a similar issue with babel-node and I'm guessing that's the same thing going on here.

callumgare avatar Jun 09 '21 10:06 callumgare

flow-remove-types works by hooking into require (via pirates), so it only works with CJS. to work with ESM, we need to use node's loaders which are still experimental and say "Note: The loaders API is being redesigned. This hook may disappear or its signature may change. Do not rely on the API described below"

So I think we'll wait until it's stable so we don't cause ourselves issues in the future. We decided to go back to require for flow-dev-tools until native ESM fully works in node. see https://github.com/facebook/flow/commit/50ee686d9e1983385075841438ee32ffa49ddea4

mroch avatar Oct 04 '21 20:10 mroch