Search code examples
javascriptfirebasegoogle-cloud-functionsfirebase-hosting

Can I locally emulate cloud function and hosting at the same time?


I am writing a simple web app with firebase hosting and cloud functions. My functions are onCreate , onDelete and httpsServer. I wan't to test my app by running it locally. How can I do this since firebase serve only works with https function and hosting.

I have tried running firebase serve and firebase functions:shell at the same time on different bash terminals. This causes firebase functions:shell to fail.

The create function :

exports.created = functions.firestore.document('Books/{bookID}')
    .onCreate((snapshot, context) => {

          FUNCTION_BODY
     });

The Delete Function :

exports.deleted = functions.firestore.document('Books/{bookID}')
    .onDelete((snapshot, context) => {

         FUNCTION_BODY
    });

The https Function :

exports.app = functions.https.onRequest(app);

The error thrown from bash :

$ firebase functions:shell
i  functions: Preparing to emulate functions.
Warning: You're using Node.js v10.13.0 but Google Cloud Functions only supports v6.11.5.
!  functions: Failed to emulate created
!  functions: Failed to emulate deleted
!  functions: Failed to emulate app
i  functions: No functions to emulate.
No functions emulated.

Output from second bash :

i  functions: Preparing to emulate functions.
Warning: You're using Node.js v10.13.0 but Google Cloud Functions only supports v6.11.5.
i  hosting: Serving hosting files from: public
+  hosting: Local server: http://localhost:5000
info: initalised
info: rendering home...
+  functions: app: http://localhost:5001/book-shelf-be347/us-central1/app
info: Worker for app closed due to file changes.

Note: These are separate bash terminals running at the same time on the same machine.


Solution

  • I did some digging through the firebase documentation and could not find any solution. This is probably because there is no official tools that let you do this. So I finally solved the problem by running the hosting using nodemon and then using

    firebase serve --functions

    This solved the problem of using same port since it is handled by nodemon.

    Hope firebase will provide new tools in the future.