Moleculer Header courtesy of MoleculerJs

How to host Moleculer in Firebase

Razvan Predescu
4 min readMay 27, 2020

--

A tutorial about hosting Moleculer micro-service framework in Firebase

Introduction

Some time ago I was playing with Moleculer, an interesting progressive micro-services framework for Node.js. For a quick introduction, you can check this tutorial written by Icebob, its creator, or discover more visiting the official Github page.

At some point, I had a request to host Moleculer in Firebase and although it looked difficult, it proved not to be so hard. The configuration is pretty basic, but it gives you an idea of how to do the job.

Note:

During my research of how to make it work, I came upon a video tutorial explaining most of the things in this article, but unfortunately I lost its track. If anyone can point me to the right direction, I would be happy to provide the author with the well deserved credentials.

Firebase Initialization

You need a Firebase account (sign up for one here) and you must install Firebase CLI on your development machine.

npm install -g firebase-tools

Create a folder for your project, navigate to it and login to Firebase with:

firebase login

Then trigger Firebase initialization by running:

firebase init

Please select both Functions and Hosting during initialization.

After scaffolding is ready, please navigate to the newly created functions folder because it is time to initialize Moleculer.

Moleculer Initialization

Now it is time to setup Moleculer.

Note:

Before doing anything, please rename packages.json from “functions” folder because it will be replaced by Moleculer version and we still need to merge the two.

You can read Moleculer First Project article to complete this part quickly, with the difference that you need to scaffold the files inside the Firebase “functions” folder created in the previous step. To do this you need to navigate to “functions” and run the following command instead of the way it is shown in the suggested article:

moleculer init project .

Firebase Update

Normally, you would like to login to Firebase management console and add a new custom domain like api.yourdomain.com to host Moleculer API static files. However, for this article I would just use the default domain Firebase creates for us.

We need all the above for our next step which is to update the default .firebaserc file created for us during scaffolding by Firebase CLI, which should look like below.

Where:

  • projects -> default -> moleculer-firebase is the ID of my Firebase project
  • hosting- > api -> moleculer-firebase is the ID of your custom domain (otr the default domain in this case)

Next, we need to update firebase.json to something simmilar with below:

Where:

  • target -> api corresponds to the “hosting -> api” from .firebaserc.
  • rewrites -> function -> moleculerApi is the name of a new Firebase function that we would create next (can be anything)

Next let’s update package.json and add the Firebase entries from the renamed package_.json before. Just add the Firebase entries to their corresponding sections and you should have something like this:

When all done, delete package_.json because we don’t need it anymore.

Note:

engines -> node should be a Firebase supported value like “8” or “10”. Unlike Moleculer, Firebase is more picky about Node.js versions

Moleculer Server

Now this is the important part that allowed me to run Moleculer in Firebase. There may be better ways, but this is the solution I came with, by using Moleculer’s as middle-ware with Express (check more details on the official page).

Add a server.js file with the below content in your “functions” folder:

We are loading Moleculer as middle-ware in Express, together with configuration and all the services.

Firebase index.js

Now it’s time to update the Firebase function. Navigate to index.js in your “functions” folder and update its content to something like below.

Where:

  • moleculerApi is the function we would create in Firebase

Now all the requests to the above Firebase function is served by Moleculer!

Lets test Moleculer API locally by running the following from inside “functions” folder:

npm install
npm run dev

Navigate to http://localhost:3000 and you should be presented with the Moleculer landing page.

Now it’s time to see it working in Firebase :)

Deploy to Firebase

From “functions” folder, please run the following command:

firebase deploy

The result should look like in the screenshot below:

If you navigate to the resulting Firebase hosting URL (in my case https://moleculer-firebase.firebaseapp.com), I hope you see Moleculer landing page below:

For your convenience, I’ve pushed a Github repository with all the above code.

Yay!

--

--