GCP Cloud Functions not looking for function.js

According to GCP doc

Cloud Functions will look for files with specific names for deployable functions. For Node.js, these filenames are index.js or function.js.

Source: https://cloud.google.com/sdk/gcloud/reference/functions/deploy#--source

In my function.js file, I have:

exports.myFunction = async (req, res) => {}

And I am deploying with this command:

gcloud functions deploy myFunction --entry-point=myFunction \
  --region=us-central1 --project=my-gcp-project

This causes this error

Function 'myFunction' is not defined in the provided module.
Did you specify the correct target function to execute?
Could not load the function, shutting down.
Error: function terminated. Recommended action: inspect logs for termination reason.

Curiously enough, the deployment works if I rename function.js to index.js.

Does anyone know what I might be missing here?

1 answer

  • answered 2021-06-18 11:48 DIGI Byte

    Following the recommended structure, you need to import all methods from relevant modules and re-export them in the index.js file so that the Virtual image can find and bind them to the appropriate functions. Without this, your functions could be simply additional code that is used in other methods as Firebase has no way to tell the difference.

    I suggest checking out the following documentation: