Firebase docs reference unknown module - node.js

Firebase has this piece of information here at https://firebase.google.com/docs/functions/locations:
Client-side location selection for callable functions Regarding the callable function, client callable setups should follow the same guidelines as HTTP functions. The client can also specify a region, and must do so if the function runs in any region other than us-central1.
To set regions on the client, specify the desired region at
initialization:
var functions = firebase.app().functions('us-central1');
I've been trying to find which node module firebase is referring to but I have had no luck.
I know that it is not 'firebase-admin' or 'firebase-functions' but thats about it.
Anyone have any ideas what this might be referring to?
Edit:
I have now also tried using this with the imports require('firebase') and require('firebase/app') (as suggested) but neither of those seem to work. I also tried generating the app with const app = firebase.initializeApp({}); and then running app.functions("region") or app().functions("region") but i keep receiving TypeErrors saying functions is not a function and app is not a function respectively.

firebase.app().functions('us-central1'); is a part of the firebase-js-sdk. With source code documented on GitHub
NPM component is #firebase/functions, that is documented on the npmjs.com.
However, it is not intended for standalone usage, and should be used along with package Firebase.
You can install it using:
$ npm i firebase

Related

Using firebase tools as a node module in firebase function

I am trying to call the export method from the firebase tools node module in a firebase function.
await firebase_tools.auth.export(tempLocalFile);
But i am receiving the following error
FirebaseError: No currently active project.
To run this command, you need to specify a project. You have two options:
- Run this command with [1m--project <alias_or_project_id>[22m.
- Set an active project by running [1mfirebase use --add[22m, then rerun this command.
To list all the Firebase projects to which you have access, run [1mfirebase projects:list[22m.
To learn about active projects for the CLI, visit https://firebase.google.com/docs/cli#project_aliases
Is there a way to set the active project in the nodejs cloud function?
Edit
I am initialising the object like so
const firebase_tools = require('firebase-tools');
I'm not exactly replying to your problem, but note that the auth.export command will not work in a Cloud Function.
As explained in the documentation (See the note at the bottom):
when used in a limited environment like Cloud Functions, not all
firebase-tools commands will work programatically because they require
access to a local filesystem.
This is the case for the auth.export command. An alternative is to use the listUsers() method of the Admin SDK, as explained here.

GCP Cloud Functions not looking for function.js

According to GCP doc
Cloud Functions will look for files with specific names for deployable functions. For Node.js, these filenames are index.js or function.js.
Source: https://cloud.google.com/sdk/gcloud/reference/functions/deploy#--source
In my function.js file, I have:
exports.myFunction = async (req, res) => {}
And I am deploying with this command:
gcloud functions deploy myFunction --entry-point=myFunction \
--region=us-central1 --project=my-gcp-project
This causes this error
Function 'myFunction' is not defined in the provided module.
Did you specify the correct target function to execute?
Could not load the function, shutting down.
Error: function terminated. Recommended action: inspect logs for termination reason.
Curiously enough, the deployment works if I rename function.js to index.js.
Does anyone know what I might be missing here?
Following the recommended structure, you need to import all methods from relevant modules and re-export them in the index.js file so that the Virtual image can find and bind them to the appropriate functions. Without this, your functions could be simply additional code that is used in other methods as Firebase has no way to tell the difference.
I suggest checking out the following documentation:
https://firebase.google.com/docs/functions/organize-functions#write_functions_in_multiple_files

how to properly initialize firebase functions test using cloud function?

I am using
Node 14
firebase-functions-test: 0.2.3
firebase-admin: 9.6.0
firebase-functions: 3.13.2
firebase tools: 9.8.0
so I want to perform unit testing for my firestore trigger function using firebase cloud function, I read the steps from the documentation in here.
I want to perform unit test using Firebase Emulator. so I assume I will initialize the SDK in offline mode. the documentation said that
If you would like to write completely offline tests, you can
initialize the SDK without any parameters:
so I initialize it like this
import * as firebase from "firebase-functions-test";
const test = firebase();
const wrapped = test.wrap(myFunctions.onCreate);
// rest of my test code
but when I run the test, I have this error:
Error: Could not load the default credentials. Browse to
https://cloud.google.com/docs/authentication/getting-started for more
information.
so even though I will use Firebase emulator (offline), it seems I need to provide the credential, which is the step if online mode is used, like explained from documentation in here
so I initialize the SDK like this
import * as firebase from "firebase-functions-test";
const test = firebase({
databaseURL: "https://xxx-b843e.firebaseio.com",
projectId: "xxx-b843e",
}, "../../../service-account.json");
const wrapped = test.wrap(myFunctions.onCreate);
// rest of my test code
but when I run the test, I have another error
{"severity":"WARNING","message":"Warning, FIREBASE_CONFIG and
GCLOUD_PROJECT environment variables are missing. Initializing
firebase-admin will fail"}
Error: The file at ../../../service-account.json does not exist, or it
is not a file. ENOENT: no such file or directory, lstat
'/Users/xxx/Documents/service-account.json'
the service account json file doesn't exist? I believe I have set the path correctly like this
in fact, I use intellisense to guide me to service-account.json path
the service-account.json file is like the image below, i get it from firebase project overview --> project settings --> service accounts --> generate new private key
what should I do if I want to initialize firebase functions test SDK in Firebase emulator?
Explanation
Error: The file at ../../../service-account.json does not exist
That error means that the service-account.json file could not be found at runtime (when you started the emulator), because the relative path is incorrect.
in fact, I use intellisense to guide me to service-account.json path
The path that intellisense suggests may not be the correct relative to the directory the code is executing in. This is because typescript is compiled before it's run, usually from within a lib or dist folder, which means the relative path needs to be changed.
Solution 1
You can use an absolute path /my/absolutepath/to/service-account.json, this is a fast way to resolve this problem, but isn't very portable.
Solution 2
Experiment with different amounts of ../../ to find the exact relative path of the service-account.json. Try using:
../../service-account.json
../../../service-account.json
etc ... (until it works)
It's most likely only off by a few ../
To use the offline mode you need to stub API calls like admin.initializeApp(). See the example given at https://firebase.google.com/docs/functions/unit-testing#importing_your_functions
// If index.js calls admin.initializeApp at the top of the file,
// we need to stub it out before requiring index.js. This is because the
// functions will be executed as a part of the require process.
// Here we stub admin.initializeApp to be a dummy function that doesn't do anything.
adminInitStub = sinon.stub(admin, 'initializeApp');
// Now we can require index.js and save the exports inside a namespace called myFunctions.
myFunctions = require('../index');
To test against the emulator you need to the initialize the test SDK with an emulator URL. Your file not found error is probably due to a problem with resolving relative paths. Try providing an absolute path and see if that works.
Getting rid of the warning should be as simple as adding the following to your jest.preset.js (or whatever code runs before your unit tests run).
const test = require('firebase-functions-test')();
https://firebase.google.com/docs/functions/unit-testing#offline-mode

Firebase functions - Failed to retrieve function source code

I get the error: "Failed to retrieve function source code" when I try and deploy a function.
This is all from the command line. I am using node 6.11.5 (but in the firebase-admin package.json file in the nodes folder it is says node 6.9.1 is used to download that). I am using firebase-admin#5.8.1 and firebase-functions#0.8.1.
This is the code in my index.js file that I am trying to deploy:
const functions = require('firebase-functions');
exports.helloWorld = functions.https.onRequest((request, response) => {
response.send("Hello from Firebase!");
});
I have also tried to deploy many different things.
Two interesting things:
- I used to be able to deploy any function without problem. This changed about a month ago and now every function I try gets this error. I can't remember making any change that would be related to this.
- Also I can deploy functions from my computer (with the exact same set up and firebase versions) to other projects in the same google account and different google accounts without any problem.
Thanks
I think you should check your billing settings in google cloud. I got the same problem and after updating billing information then redeploy the function, the error is gone.

Using a Node.js class in Cloud Functions for Firebase

I am a new to Node.js and Firebase.
I have successfully tried to deploy some Cloud Functions to test them a bit.
I have a Node.js project in which I have a class defined as:
import * as Api from './api';
export default class MyClass {
constructor(props) {[...]}
someFunction(props) {
return Api.someOtherFunction(props.arg1).then([..]).catch([..]);
}
}
In the Api code I use the firebase admin SDK and I work with the real time database.Ex.:
ref.child(`users/${userId}`).set({
id: userId,
arg1: arg1,
arg2: arg2
});
Now, the problem is that I would like to use MyClass in a cloud function.
I have read a lot about ES6 in Cloud Functions, too (ex.: here), but I am not able to get rid of the error message
SyntaxError: Unexpected token import
I have tried to convert to require statements but I am not able to require my local module where MyClass is.
I don't care if it will be a Node.js local module or simply some classes in clear hierarchical structure.
What I would like to ask is if there is a specific documentation about this situation (I have searched a lot for it) and/or if I am following the right way to structure my project.
If the answer is "NO", please give me some tips on how to structure it.
Cloud Functions (node.js) currently does not support the ES6 import syntax, you have to use require().
The examples in the article you linked are using Babel to convert ES6 to ES5. Even if you convert your import statements to require() you're going to encounter the fact that ES5 does not support the class syntax. If you want to do what the article it doing, follow the steps to get Babel converting your ES6 script to ES5 before sending it to Firebase Cloud.

Resources