how to test Cloud Functions for Firebase locally on pc

前端 未结 9 1446
耶瑟儿~
耶瑟儿~ 2020-11-27 16:39

Today Firebase released its brand new product Cloud Functions for Firebase and I just created a hello world function and deploy it on my existing firebase project.

I

相关标签:
9条回答
  • 2020-11-27 16:55

    run and debug/inspect functions locally

    prerequisites (google-cloud functions and firebase-specific):

    npm install -g @google-cloud/functions-emulator
    npm install --save firebase-functions
    npm install -g firebase-tools
    

    To run and inspect/debug: first run functions locally, then inspect each function, and finally run each specific function to debug+inspect it. Use functions start as an alternative to firebase serve and note the documentation for each tool is available (and useful).

    To run and debug the specific function myFn as-expected (eg in Nodejs via chrome://inspect and note this works using Nodejs v10 though not officially supported):

    firebase serve --only functions
    functions inspect myFn
    functions call myFn # or call from browser
    

    additional documentation:

    https://firebase.google.com/docs/functions/local-emulator https://cloud.google.com/functions/docs/emulator#debug-emulator https://github.com/GoogleCloudPlatform/cloud-functions-emulator/wiki

    0 讨论(0)
  • 2020-11-27 16:56

    There is now a cloud functions emulator that lets you call functions locally

    Once I have completed my PoC I will update this answer to include code and steps I used.

    0 讨论(0)
  • 2020-11-27 16:57

    >> Is there any way to test Cloud Functions for Firebase locally?

    You can use the following command to start a firebase shell (execute in your functions directory):

    npm run build && firebase functions:shell
    

    You can invoke your functions in the shell like so:

    helloWorld()
    

    Refer this link for more information.

    0 讨论(0)
  • 2020-11-27 17:05

    For vscode users debugging HTTP functions (webhooks, etc)...

    The google cloud emulator (firebase serve --only functions) launches a separate process to run your functions. You can attach to this process with vscode, but since the emulator only creates this process after the first function is called, it's not straightforward.

    • create a dummy HTTP endpoint in your functions which will return the processID:
    app.get("/processid", function(request, response) {
      response.send(`${process.pid}`);
    });
    
    • start the emulator with firebase serve --only functions
    • call the http://<localhost_url>/processid endpoint. This will create the process and return the processID
    • use vscode to attach to the specified process. You can now set breakpoints, step, etc on any of the other functions (they all use the same process).

    There's probably a nicer way to glue all this together.

    0 讨论(0)
  • 2020-11-27 17:10

    Answered here: https://github.com/firebase/firebase-functions/issues/4#issuecomment-286515989

    Google Cloud Functions also open-sourced a local emulator, and we are working to build a tighter integration with Cloud Functions for Firebase. In the meanwhile, you can check it at here: https://github.com/GoogleCloudPlatform/cloud-functions-emulator/

    The emulator does allow you to run functions locally. Here's the documentation that explains how to use it: https://cloud.google.com/functions/docs/emulator

    0 讨论(0)
  • 2020-11-27 17:11

    Firstly, I suggest you to install following dependencies,

    npm install --save firebase-functions
    npm install -g firebase-tools 
    

    If already installed then you can update it to latest one. Generally, functions-emulator comes with above dependency but still I would recommend you to update it,

    npm install -g @google-cloud/functions-emulator
    

    Once it has been updated, go to functions folder of you application and run following command,

    firebase serve --only functions
    

    I hope it helps!

    0 讨论(0)
提交回复
热议问题