Running Serverless Node.js on Google Cloud Functions

Running Serverless Node.js on Google Cloud Functions

Running Serverless Node.js on Google Cloud Functions

Google Cloud Functions is a service that allows you to run your code in the cloud. You don't need to manage server, just code for the application logic (serverless). It's also highly available and fault tolerant, as well as very automatically scales. There are some ways to trigger function execution, some of which are the result of integration with other Google Cloud services. In this tutorial, I'm going to show how to run serverless Node.js in Google Cloud Functions.


  1. Create or select a Google Cloud project

A Google Cloud project is required to use this service. Open Google Cloud console, then create a new project or select existing project

  1. Enable billing for the project

Like other cloud platforms, Google requires you to enable billing for your project. If you haven't set up billing, open billing page.

  1. Enable Cloud Functions API

To use an API, you must enable it first. Open this page to enable Cloud Functions API.

  1. Set up service account for authentication

You need to install gcloud on your computer and get authenticated to access your account.

Writing a Function

First, create a folder for this project. Inside the folder, create a file with the following content.


  exports.helloWorld = (req, res) => res.send("Hello, World!");

Running Google Cloud Functions Emulator

Before running functions on the real Google Cloud servers, we can test whether the code works as expected by running Google Cloud Functions Emulator on your local machine.

  • First, you need to install the emulator using npm install -g @google-cloud/functions-emulator
  • Run it using functions start.
  • To deploy the helloWorld function we have created on the previous step, run functions deploy helloWorld --trigger-http
  • If the function successfully deployed, you will see a resource URL - open it to call the function

    Deploying to Google Cloud Functions

Run the following command in order to deploy the functionn to Google Cloud Functions.

gcloud functions deploy helloAsync --trigger-http --project PROJECT_NAME

Below is the list of supported flags while running gcloud functions deploy command.

gcloud functions deploy (NAME : --region=REGION)
            [--entry-point=ENTRY_POINT] [--memory=MEMORY] [--retry]
            [--runtime=RUNTIME] [--source=SOURCE] [--stage-bucket=STAGE_BUCKET]
            [--timeout=TIMEOUT] [--update-labels=[KEY=VALUE,...]]
            [--clear-env-vars | --env-vars-file=FILE_PATH
              | --set-env-vars=[KEY=VALUE,...]
              | --remove-env-vars=[KEY,...] --update-env-vars=[KEY=VALUE,...]]
            [--clear-labels | --remove-labels=[KEY,...]]
            [--trigger-bucket=TRIGGER_BUCKET | --trigger-http
              | --trigger-topic=TRIGGER_TOPIC
              | --trigger-event=EVENT_TYPE --trigger-resource=RESOURCE]
            [GCLOUD_WIDE_FLAG ...]

NAME is the ID of the function or fully qualified identifier for the function. Description for each flags is on the table below

Installing Dependencies

To install a module, add it to the dependencies of package.json. Google will automatically install all dependencies at the deployment.

Alternatively you can run npm install MODULE_NAME, which will update package.json content.

Using Node.js 8 Environment

Currently Google still uses Node.js 6 as the default version. With that version, you can't use some new features such as async/await. But Google makes it possible for use to use Node.js 8. First, add a function that won't run on Node.js 6, as examplified below.


 const request = require('request-promise');

  exports.helloAsync = async (req, res) => {
    const result = await request('');

    return res.send(result);

To use Node.js 8 runtime environment, add --runtime nodejs8 parameter to the command.

gcloud functions deploy helloAsync --trigger-http --project PROJECT_NAME --runtime nodejs8

Angular 9 Tutorial: Learn to Build a CRUD Angular App Quickly

What's new in Bootstrap 5 and when Bootstrap 5 release date?

Brave, Chrome, Firefox, Opera or Edge: Which is Better and Faster?

How to Build Progressive Web Apps (PWA) using Angular 9

What is new features in Javascript ES2020 ECMAScript 2020

Top Node.js Development Companies and Expert NodeJS Developers

A thoroughly researched list of top NodeJS development companies with ratings & reviews to help hire the best Node.JS developers who provide development services and solutions across the world. List of Leading Node.js development Service Providers...

Hire Node JS Developer from Expert Node JS Development Company

NodeJS Development Company-Hire Node JS developer from the most prominent NodeJS development company, Mobiweb and get remarkable Node.js app development services.

Main Reasons of Using Node JS for Your Web Application Development

You have to hire Node JS developer from prestigious and expert Node JS development company Mobiweb Technologies. They are tech enthusiasts with new and latest programming ideas, web development technologies and industry trends.