node.jsgoogle-cloud-functionscloud

Google function framework cannot run structured cloud function locally


I have a Google Cloud Function written in nodejs, which I want to run with @google-cloud/functions-framework. My structure is

main_folder/ 
 test_function/ 
   index.js 
 node_modules/ 
 package.json

So as in the structure, my entry point is inside test_function/index.js. Is there a way to call that function using function framework ? or function framework does not work with folder structure (index.js has to be in the root folder) ?

Any help would be appreciated. Thanks in advance


Solution

  • Suppose your test_function/index.js file has an entry point function as follows

    export async function handler(req, res) {
      return res.status(200).send("Success")
    }
    

    To run the function which is inside a folder, in the package.json set the main field as

      "main": "test_function/index.js"
    

    And add the npm start script as

        "start": "npx @google-cloud/functions-framework --target handler"
    

    Rememeber handler is the name of the entry point function which you export.

    This will work