0

I have a monorepo where my cloud functions import from another package in the workspace. The package that is imported is in the package.json as part of the devDependencies like so:

// stuff
 "dependencies": {
 "@app/config": "../../packages/config/dist/cjs",
}
// more stuff

When deploying with the CLI using firebase deploy I end up with the following error message:

Function failed on loading user code. This is likely due to a bug in the user code. Error message: Provided module can't be loaded. Did you list all required modules in the package.json dependencies? Detailed stack trace: Error: Cannot find module '@app/config'

Since I am working in the context of a npm workspace, the dependency in the cloud functions node_modules is symlinked, not actually installed. So I assume that when the CLI uploads the code and tries to build it in Google Cloud, the link points to a dead end and the build breaks.

After a bit of digging it seems that my two options are:

  • publish my package on a (private) registry: github, npm etc.
  • add a build step to npm pack my dependency and copy it to the cloud functions code base. I assume that this could likely be done with the predeploy hook in the firebase.json and a custom script in the repo.

Is there any (simpler) option? And am I on the right track?

Thanks

Stf_F
  • 846
  • 9
  • 23
  • Have a look at this [Stackoverflow Link](https://stackoverflow.com/q/57342258/18265702) and [Github Link](https://github.com/firebase/firebase-admin-node/issues/1488). – Sandeep Vokkareni May 22 '23 at 08:29
  • Thanks but none of these issues are related to mine. I don't have a problem with firebase modules, but with a private symlinked package of the monorepo. – Stf_F May 22 '23 at 09:09

1 Answers1

0

Turns out that with the rise of mono repos fuelled by npm and yarn workspaces this issue has become increasingly popular and is documented at length in this 5 years old issue on GH: https://github.com/firebase/firebase-tools/issues/653

This issue is indeed caused by a limitation of firebase tools that assume that all dependencies are published to a registry.

There are no official solutions for now and unlikely to be as according to the google employees there, the changes would impact too many layers.

So the solution is either:

  • to publish to a private registry
  • npm pack
  • use rollup, webpack etc. to bundle the functions before deployment.

For option 2 (pack), there are a few packages that have been made by some community members to address that and also some ad hoc scripts that can be executed via the predeploy hook, all available in the GH thread.

I have resorted for now to npm pack my dependencies and update my package.json to refer to the tarball like so:

"dependencies": {
  "@app/internalpackage": "file:./tmp/internalpackage-x.x.x.tgz"
}

It does the job.

Stf_F
  • 846
  • 9
  • 23