Search code examples
node.jsgoogle-cloud-platformgoogle-cloud-functionsfunctions-framework

Test GCP Cloud Function locally with a different file


I have a GCP Cloud Function that works just fine. I invoke it locally like this:

npx @google-cloud/functions-framework --target=getData

This command launches an http trigger as http://localhost:8080.

The command does not have any parameters to specify the file to use. It uses index.js.

I would like to test with a copy of index.js that I have named test.js.

How can I launch test.js with Functions Framework?


Solution

  • My guess is that it's highly likely that the framework is using the node module's entrypoint as defined in the main attribute of your package.json. Typically that is some path to index.js, but you can change it to whatever file you want.

    The only documented options to change where the framework looks for file is --source, which just changes the source folder (which probably still requires a package.json with a main entrypoint).

    One final thing to consider is the deeply embedded documentation on using TypeScript, which shows a clear example of setting main to different file (emphasis mine):

    Update your package.json with scripts for building and running your application:

    {
      "main": "dist/index.js",
      "scripts": {
        "build": "tsc",
        "start": "functions-framework --target=TypescriptFunction",
        "prestart": "npm run build",
        "gcp-build": "npm run build"
      },
      ...
    }
    
    • The main field must be configured to the compiled source code file that contains your function source.