What is @firebase/functions?
The @firebase/functions package is part of the Firebase JavaScript SDK, providing a powerful, secure, and scalable way to call Cloud Functions for Firebase directly from a web or mobile client. It allows developers to trigger backend code without managing servers, which can be written in Node.js and automatically runs in a managed environment on Google Cloud.
What are @firebase/functions's main functionalities?
Calling a Cloud Function
This feature allows you to call a Cloud Function named 'sayHello' from your client-side application. The function takes an object as an argument, in this case, { name: 'Firebase' }, and returns a promise that resolves with the result of the function call.
const functions = require('@firebase/functions');
const sayHello = functions.httpsCallable('sayHello');
sayHello({ name: 'Firebase' }).then(result => {
console.log(result.data);
});
Handling Errors
This demonstrates how to handle errors when calling a Cloud Function. The error object contains details about what went wrong, which can be used for debugging or displaying an appropriate message to the user.
sayHello({ name: 'Firebase' }).catch(error => {
const code = error.code;
const message = error.message;
const details = error.details;
// Handle the error
});
Other packages similar to @firebase/functions
aws-sdk
The AWS SDK for JavaScript allows you to directly interact with AWS services from client-side code. Similar to @firebase/functions, it enables calling cloud functions (AWS Lambda) but within the AWS ecosystem. The main difference lies in the cloud provider and the specific services and features offered by AWS versus Firebase.
azure-functions
Azure Functions is a package that allows you to develop, test, and run Azure functions locally before deploying them to the cloud. While it serves a similar purpose in enabling serverless computing, it's more focused on the Azure platform, contrasting with @firebase/functions which is tailored for Firebase and Google Cloud.
@firebase/functions
This is the functions component for the Firebase JS SDK. It has a peer
dependency on the @firebase/app
package on NPM. This package
is included by default in the firebase
wrapper
package.
Installation
You can install this package by running the following in your project:
$ npm install @firebase/functions
Usage
You can then use the firebase namespace exposed by this package as illustrated
below:
ES Modules
import firebase from '@firebase/app';
import '@firebase/functions'
CommonJS Modules
const firebase = require('@firebase/app').default;
require('@firebase/functions');
Documentation
For comprehensive documentation please see the Firebase Reference
Docs.