Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flow: resolving modules in a monorepo that uses Yarn workspaces

We have a monorepo that uses Yarn’s ‘workspaces’ feature, meaning that whenever possible, Yarn will hoist dependencies to the monorepo's root node_modules directory rather than keep them in the individual package's node_modules dir. This relies on Node’s module resolving algorithm, which continues to search for modules in node_modules directories up the dir tree until it finds the required module.

When using Flow types in a file that imports another package (internal or external to the monorepo), running Flow inside the package that contains that file causes a Cannot resolve <package-name> error to be thrown. It seems like Flow uses a different module resolving algorithm, and fails since the installed modules are hoisted to the root dir and Flow does not continue to search up the dir tree.

Is there a way around this other than running Flow from the root? Running from the root is less than optimal because it does not allow different settings for different packages in the monorepo.

Node version: 10.8.0
flow-bin version: 0.78.0

like image 926
EranM Avatar asked Aug 13 '18 14:08

EranM


People also ask

How to run commands in workspaces in a monorepo?

With workspaces since the dependencies are locked from root, you just need to do a yarn at the top-level. For workspaces to work, your “workspace” folders need to have a package.json that contain a name and version. To run commands in a single package in your monorepo, use the following syntax:

Is a monorepo the right tooling for You?

Previously, we wrote about monorepos and how Yarn Workspaces makes working with them simpler. Unfortunately, moving to a monorepo is not always an easy choice. Without the right tooling, a monorepo can often harm the developer experience instead of help it.

Is yarn monorepo or parallel?

It’s monorepo. If you’re not aware of monorepo benefits and flaws check this article out. Yarn is fast. Yarn stores packages in a cache. Yarn is parallel. But what about Yarn workspaces? That’s how Yarn explains them:

How do I run a node test in a monorepo?

To run commands in a single package in your monorepo, use the following syntax: $ yarn workspace example-1 run test yarn workspace v1.x.x yarn run v1.x.x $ node test.js test from example 1 ✨ Done in 0.23s. ✨ Done in 0.86s.


1 Answers

I also ran into this problem

To fix it need update .flowconfig:

[include]
../../node_modules/

FS struct:

/project_root
--/node_modules
--/packages
----/module1
------.flowconfig
like image 139
DAVID _ Avatar answered Sep 18 '22 16:09

DAVID _