2

I have 2 firebase functions project: prod and infra. infra needs all the functions of prod and has some more of its own (i.e. infra is a strict superset of prod). The directory structure is roughly:

|---> myDirectory
   |---> infra
   | |---> functions
   |   |---> src
   |     |---> index.js          // infra is TS
   |---> prod
     |---> functions
       |---> index.js            // prod is JS

How do you import functions from prod into infra? I've tried

    // myDirectory/infra/functions/index.js
    const { functionA } = require('../../prod/functions/index.js');
    exports.functionA = functionA;

This works fine on emulators, but I'm getting this error when deploying:

validateEventDocument 
Provided module can't be loaded. 
Did you list all required modules in the package.json dependencies? 
Detailed stack trace: Error: Cannot find module '../../../prod/functions/index.js' 
Minh Lam
  • 61
  • 4
  • 1
    On Stack Overflow, please don't show pictures of text. Copy the text into the question itself so it's easy to read, copy, and search. – Doug Stevenson Aug 02 '20 at 23:45

1 Answers1

0

If you have both files, within different locations with the same name, this could be causing an issue for your import. So, I would recommend you to have different names for the files, at least, since all the directory structure is the same. As clarified in this other similar case here, the way you are importing is basically correct, but your structure is not helping the Cloud Functions to be imported in your project. I would recommend you to change it to something like below and then try to import them.

|---> myDirectory
   |---> infra
   | |---> i_functions
   | |  |---> f_index.js
   |---> prod
   | |---> p_functions
   | |  |---> p_index.js

// myDirectory/infra/functions/index.js
    const { functionA } = require('../../prod/p_functions/p_index.js');
    exports.functionA = functionA;

You also, might want to check your firebase.json file, to confirm if it's configured to indicate the Cloud Functions you need.

Besides that, the below two similar cases, might provided you with some insights on how to better organize and structure your different Cloud Functions files.

Let me know if the information helped you!

gso_gabriel
  • 4,199
  • 1
  • 10
  • 22
  • To clarify, my projects are actually called `prod` and `infra`. I don't know why I chose to present it as production on Stack Overflow. Let me edit the question to clarify that misleading detail. Let me also try renaming the index files and see if that works – Minh Lam Aug 04 '20 at 00:53
  • Renaming the index files doesn't work. I think the root cause is because `infra` has its own package.json, .firebaserc and firebase.json, so when I run `firebase deploy`, the code of `myDirectory/prod/functions/index.js` is not uploaded. [This article on requiring local modules](https://firebase.google.com/docs/functions/handle-dependencies#including_local_nodejs_modules_as_part_of_your_deployment_package) is promising, but not very clear. Do you know where I can find more info about requiring local modules? – Minh Lam Aug 04 '20 at 01:08
  • Hi @MinhLam thanks for confirming about the indexes. Indeed, your `firebase.json` is an important file that you need to check - I thought you had already checked that. Besides that, I don't think that it's related to local modules, since this is a different feature. In case you think my answer is being helpful, please, consider upvoting it. – gso_gabriel Aug 04 '20 at 05:47
  • 1
    Note that the Firebase CLI will not deploy anything outside of its functions folder. It will not merge content from folder sources after deployment. All the code must be in there, or come from node_modules (only from one project), or have some explicit source defined in package.json. – Doug Stevenson Aug 04 '20 at 06:03
  • @DougStevenson That's right, that's what I have found out. I wish it does that automatically, but it seems the only thing I can do right now is restructuring the directory system. – Minh Lam Aug 09 '20 at 17:43