'Yarn workspaces monorepo with Firebase functions failed to initialize

I am trying to set up a monorepo that firebase functions will be able to require some shared logic from different directories - as for now I have a setup that works for me:

packages
├─firebase
│ ├─functions
│ │ ├─index.js -- (firebase function entry point)
│ │ └─package.json -- (name: '@functions') 
│ ├─firebase.json
│ ├─.firebaserc
│ └─package.json -- (name: '@firebase')
└─front (react app inside)
  ├─src
  ├─package.json -- (name: '@front')
  └─webpack.config.js

The root project package.json:

 "private": true,
 "workspaces": [
     "apps/**",
     "lib/**"
 ],

What I want to achieve is that I will be able to split the firebase directory like so:

├─packages
│ ├─functions -- (will require the config file from firebase workspace inside lib directory)
│ │ ├─index.js (firebase function entry point)
│ │ └─package.json
│ └─front (react app inside)
│   ├─src
│   ├─package.json
│   └─webpack.config.js
└─lib
  └─firebase
    ├─.firebaserc
    ├─firebase.json
    └─package.json

I tried to add to the '@functions' package.json "dependencies": "@firebase": "*" but no luck and when I try to run the firebase emulator I get:

The functions emulator is configured but there is no functions source directory. Have you run firebase init functions?
Could not find config (firebase.json) so using defaults.

Functions package does not locate the firebase.json and .firebaserc?

How can I achieve packages splitting with firebase in the monorepo?



Solution 1:[1]

This project structure would not work currently with the Firebase CLI. While using the emulator works by running firebase emulators:start --only functions inside the lib/firebase workspace, and having firebase.json point to the functions source workspace, deploying this function will throw a different error:

Error: ../../packages/functions is outside of project directory

This is because it's intentional that the firebase.json and .firebaserc files be located on a parent directory in relation to where the source folder is located.

The Firebase CLI also assumes all your dependencies in the functions package.json are hosted on npm. Adding the lib/firebase workspace as a dependency will result in a "not found" error.

This could change in the future as there is a feature request open to officially support mono-repos. This request also has quite a few workarounds you can try for now.

Something I tried while replicating your project was simply moving the firebase.json and .firebaserc files to the top of the project. This allows emulators and deployments to work using firebase commands or yarn scripts pointing to those commands.

??packages
????functions
??????index.js
??????package.json
????front (react app)
??????src
??????package.json
??????webpack.config.js
?? firebase.json
?? .firebaserc
?? node-modules (project-level)
?? other project-level files...

All libraries are located in the top level node_modules through your workspaces though, which is a core feature of mono-repos. Let me know if this was useful.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 ErnestoC