Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading