1

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?

Niv
  • 523
  • 1
  • 8
  • 19
  • Does this answer your question? [Deploying firebase functions with local depencies using firebase CLI](https://stackoverflow.com/questions/66239777/deploying-firebase-functions-with-local-depencies-using-firebase-cli) – Thijs Koerselman May 09 '23 at 10:47

2 Answers2

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.

ErnestoC
  • 2,660
  • 1
  • 6
  • 19
  • I actually could deploy it but the `firebase.json` and `.firebaserc` were placed in the `packages` folder - as you mentioned they should be in a parent relation to the `functions`. I could not figure out how to use shared logic via workspaces inside the `functions` (if there is a possible workaround) - for example using util functions in the `functions` and in `front` as well. – Niv May 11 '22 at 20:00
  • All local code you'd want to use in your Function must be in the `functions` directory [during deployment](https://stackoverflow.com/a/46567563/16929894). One workaround would be to [use scripts that move](https://github.com/firebase/firebase-tools/issues/653#issuecomment-1113650277) your code into the functions directory only during deployment, and then revert the changes. There's also the option to publish the packages to npm, which would work the same as packages already hosted there. – ErnestoC May 16 '22 at 17:26
0

This question is basically a duplicated of Deploying firebase functions with local depencies using firebase CLI

The problem is that Firebase tools deploy doesn't understand / support monorepos, regardless of chosen package manager Yarn, NPM or PNPM. I have recently developed a solution for this, and posted it in the answer in the linked question above.

Thijs Koerselman
  • 21,680
  • 22
  • 74
  • 108