I know someone else has posted this question before, but I believe mine has a right declaration and still doesn't work:

Running the sendEmail.js:

node sendEmail.js

Inside sendEmail.js:

"use strict";
// Initialize Function Caller App
const firebase = require("firebase");
// Required for side-effects
require("firebase/functions");

var config = {
  apiKey: "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
  authDomain: "my-project-id.firebaseapp.com",
  databaseURL: "https://my-project-id.firebaseio.com",
  projectId: "my-project-id",
  storageBucket: "my-project-id.appspot.com",
  messagingSenderId: "xxxxxxxxxx"
};
firebase.initializeApp(config);

var functions = firebase.functions(); // Error here: TypeError: firebase.functions is not a function

I got the above sample from the Firebase Documentation. It works for my other project but not the current one. I've also run the following in my sendEmail.js folder:

npm install [email protected] --save

In my Firebase project root folder, my package.json file has the firebase-functions dependency too:

"dependencies": {
    "firebase": "^5.10.0",
    "firebase-admin": "^7.3.0",
    "firebase-functions": "^2.3.0",
    "nodemailer": "^6.1.0"
  },

But FYI, my sendEmail.js is in the following folder:

project-root\testing\sendEmail.js
project-root\testing\node_modules\@firebase
project-root\testing\node_modules\@firebase\functions

And I still get this error:

var functions = firebase.functions();
                         ^
TypeError: firebase.functions is not a function
    at Object.<anonymous> (d:\project-root\testing\sendEmail.js:17:26)
    at Module._compile (module.js:652:30)
    at Object.Module._extensions..js (module.js:663:10)
    at Module.load (module.js:565:32)
    at tryModuleLoad (module.js:505:12)
    at Function.Module._load (module.js:497:3)
    at Function.Module.runMain (module.js:693:10)
    at startup (bootstrap_node.js:191:16)
    at bootstrap_node.js:612:3

May I know what else do I need to check? Any help will be much appreciated, thank you!

1 Answers

0
Frank van Puffelen On

The code you have was made to run in Cloud Functions for Firebase. You can't simply run it in a local Node.js, as it depends on things that won't be available there.

To run the code in Cloud Functions, you have to deploy it to Google's servers with the firebase deploy command.

To test the functions locally, use the firebase serve command.