1

I've NodeJS project created on firebase cloud functions that contain our backend service ( ExpressJS ) as http function and some other crons functions, The project structure is like this :

/project ( home dir for all cloud function)
    - package.json
    - index.js ( contain the app backend funciton and other crons functions)
    - /src
        - /lib
        - /methods
        - routes.js ( loaded by app backend service )
        - /crons ( loaded in index.js )
            - funA.js
            - funB.js
        ...etc

And all the functions do make call to the lib & methods functions and some other helper functions, So when all deployed all files are deployed with it.

So now we moving to Cloud run, So im going to convert the app backend service from cloud function to docker container to run on cloud run.

Because now we have 2 different service, Cloud run for backend and cloud functions for crons, The new project structure:

/project 
    - package.json
    - index.js ( serve only express backend app )
    - DockerFile
    - /src
        - /lib
        - /methods 
        ...etc
    - /functions ( home dir for all cloud function)
        - package.json
        - index.js ( load crons functions )
        - /crons
            - funA.js
            - funB.js

The issue now is that crons make call to /lib and /methods, But when deploy to cloud functions it will not be included because its out of the app ( functions dir ).

Possiable solution for me is to just move out the /functions to new project and copy all methods / libs into it.

but this will be double work because we always make updates to /lib and /methods

Any suggest solutions ?

Ibrahim.Sluma
  • 164
  • 13
  • Have you checked this [StackOverflow thread](https://stackoverflow.com/a/42590760/18265570)? You can use the [require()](https://cloud.google.com/functions/docs/writing/specifying-dependencies-nodejs#loading_nodejs_modules) function to import local files you deploy alongside your function. – Roopa M Oct 23 '22 at 11:56
  • 1
    @RoopaM yes I can local require them, but when deploy to cloud function it will not be included because they our of the project level – Ibrahim.Sluma Oct 23 '22 at 13:07
  • Can you have a look at this [thread](https://stackoverflow.com/q/48916633/18265570)? – Roopa M Oct 25 '22 at 06:05
  • yep same, The issue is that when I'm import some module outside of the functions dir when deploy it doesn't included. – Ibrahim.Sluma Oct 25 '22 at 11:16

1 Answers1

2

As mentioned in this github link, GCF uses Cloud Build to install your dependencies and build your functions code.

Due to this limitation, you can't import things that don't live inside your functions directory. If you need to maintain a directory structure like this, you'll need to treat code outside of your functions directory as a separate module, and either make it available through npm, or package it and copy it over to your functions folder.

Roopa M
  • 2,171
  • 4
  • 12
  • For now the only solution I done and I don't like is that I've replicated the part of the modules that im using in crons, so I don't ship unused code to cloud functions. – Ibrahim.Sluma Oct 26 '22 at 21:08