GoogleCloudPlatform / functions-framework

The Contract for Building New Function Frameworks
Apache License 2.0
120 stars 12 forks source link

How to specify a FUNCTION_TARGET which is not defined at root source file? #41

Closed manwithsteelnerves closed 3 years ago

manwithsteelnerves commented 3 years ago

When working with functions, its quite common for having the function code maintained in a hierarchy. Currently my triggers code is not at the root source file but deep inside a hiearchy.

In the index, i refer it like this exports.triggers = require('./common/external/database/triggers');

    "start": "cross-env FUNCTION_TARGET=triggers-level0CollectionDocumentOnWrite cross-env FUNCTION_SIGNATURE_TYPE=event functions-framework",

I'm getting an error as below

Supporting generic firestore collection document triggers for 4 collection levels only
Function 'triggers-level0CollectionDocumentOnWrite' is not defined in the provided module.
Did you specify the correct target function to execute?
Could not load the function, shutting down.

Question

How to let functions framework detect the function whose source code is deep down the folders?

grant commented 3 years ago

Hi, I'm not sure if you're using a specific language (like Node), or asking a question in general in this generic repo.

The Functions Framework must target a function in your root directory to where you start the framework, like an index.js file. It's common to have that index file refer/require to a nested folder like './common/external/database/triggers.

You can also use FUNCTION_SOURCE in some languages to target deep down the folders. However, I recommend 1 top-level index file to manage things.

LMK if that helps or please provide more details in the question including the language.

manwithsteelnerves commented 3 years ago

Thanks @grant for the reply! Yes, I'm using Node.

I was able to solve this by using '.' instead of '-'

"start": "cross-env FUNCTION_TARGET=triggers**.**level0CollectionDocumentOnWrite cross-env FUNCTION_SIGNATURE_TYPE=event functions-framework"

The confusion was because firebase creates the function names with '-'.