New runtime configuration options with Cloud Functions for Firebase

Recently, at Cloud Next 2018, we saw some hotly anticipated updates to Cloud Functions, and in particular, the general availability of Cloud Functions for Firebase. As part of that release, there are some additional configurations you can use in your functions when deploying with the Firebase CLI. Let’s take a look!

Node.js 8 runtime now available

You can now target Node.js 8 as a runtime option (in beta, targeting version 8.11.1). What’s in it for you? Most notably, Node.js 8 offers performance improvements with the V8 JavaScript runtime, and the much-desired ability to use ECMAScript 2017 async/await syntax with native JavaScript, without having to transpile. You can read more about async functions here.

You can get started deploying functions to the Node.js 8 runtime by updating the Firebase CLI to at least version 4.0.0. To get the latest:

$ npm install -g firebase-tools@latest
$ firebase --version
4.0.0

You’ll also need to be using at least version 2.0.1 of the firebase-functions module in your project:

$ npm install firebase-functions@latest

After that, you’ll need to make a small change to your project’s configuration in package.json. Simply add the following key and value at the top level of the JSON configuration:

"engines": { "node": "8" }

Now, when you run firebase deploy, the CLI will give you some feedback about the deployment runtime in its output:

i  functions: updating Node.js 8 function onMessageCreate(us-central1)...
i  functions: updating Node.js 8 function onMessageDelete(us-central1)...

For those of you using TypeScript to write your functions (and I really think you should!), you can also update your TypeScript configuration in tsconfig.json to target ECMAScript 2017 instead of ES6. Your new configuration will have a compilerOptions block that looks something like the following. Note that target and lib values are now changed to “es2017” instead of “es6”.

  "compilerOptions": {
    "target": "es2017",
    "lib": ["es2017"],
    "module": "commonjs",
    ...
  }

With this, you’ll notice that your transpiled TypeScript no longer converts async/await to JavaScript generators, meaning it’ll be a bit tighter and faster.

Google Cloud Functions also now allow developers to build with Python, however, the Firebase CLI does not currently support that runtime.

New runtime configuration options

Using the Firebase CLI, you can specify regions per-function to bring your serverless infrastructure closer to your customers or other cloud infrastructure. You can also specify memory allocation and timeout on a per-function basis, tailoring performance to your use cases.

You can do this with the firebase-functions module that you’re already using to build your functions, and you can apply these configurations to any type of trigger. Note the new region and runWith methods in this code sample:

const functions = require('firebase-functions');

exports.myHttpFunction = functions
  // Choose a region other than the default us-central1
  .region('europe-west1')
  // Increased memory, decreased timeout (compared to defaults)
  .runWith({ memory: '1GB', timeoutSeconds: 120 })
  .https
  .onRequest((req, res) = > {
     // time and memory intensive tasks
     res.send('Hello world');
  });

Version 2.0.1 or later of the firebase-functions module is required.

Getting started with Cloud Functions for Firebase

If you’re just getting started with the Firebase SDKs for Cloud Functions, try following our step-by-step codelab and visiting the documentation. We also have a video tutorial to help get you set up using TypeScript:

We hope you find these new functionality helpful, and we look forward to continue giving you more features to help you build truly serverless apps!