We're starting to adopt a monorepo setup using yarn workspaces and we'd like to have our firebase functions inside it. The repo structure is something like:
repo
node_modules <- all dependencies
packages
core
commom
functions <- firebase functions
So, I have 2 problems with this setup:
- The dependencies of the functions don't live on the same folder as the entry file from functions
- The functions depends on other packages such as
core
andcommom
that are in the repo so yarn symlinks from node_modules to the packages in the repo.
Is there anyway I can handle this?
I am not sure I understand the question exactly, but I could give you my two cents on yarn workspaces based on whatever I understood from your question and from my experience using it.
Yarn workspaces consolidate all your dependencies into the node_modules present in project root as well as in a single package-lock.json to reduce conflicts and enables yarn to optimize the installation process giving you a faster
yarn install
. And also another advantage of it is, with a single passyarn install
can install dependencies of all packages under the workspace.Edit: I think for some reason
yarn link
is not being called and instead onlyyarn install
is being run, which will search the npm registries and throws the error mentioned in comment since it can't find the mentioned package on npm registry. So for a solution try creating an entry in the firebase's package.json likeThe solution I found for this is Yarn's
nohoist
option in your rootpackage.json
file.By default Yarn hoists dependencies to the root directory so they can be shared between your packages. Unfortunately this will not work with Firebase. This means you need to tell Yarn not to hoist the dependencies used by your Firebase functions.
The documentation for
nohoist
is less than ideal, but here is an official blog post about it here: https://yarnpkg.com/blog/2018/02/15/nohoist/You probably want something like this:
Keep in mind that this uses the
name
field used in thepackage.json
files of each workspace package. So in this example, it is assume that thefunctions
directory has apackage.json
with "functions" as it'sname
.functions/**
tells yarn not to hoist any of the dependencies specified inpackages/functions/package.json
. This doesn't work for your shared yarn packages though, sofunctions/core
andfunctions/common
need to be specified separately.You also need to include your workspaces as dependencies in your
functions
project, so add them to yourpackage.json
:Once you have added all that, you should delete your
packages/functions/node_modules
directory and runyarn install
. After doing this, you should see all your dependencies included inpackages/functions/node_modules
(not symlinks).