0

I am trying to use Firebase SDK on my Google App Script app. The documentation says it needs installing npm install firebase which is not possible via Google App Script.

So the following step would be:

// Import the functions you need from the SDKs you need
import { initializeApp } from "firebase/app";
import { getAnalytics } from "firebase/analytics";
// TODO: Add SDKs for Firebase products that you want to use
// https://firebase.google.com/docs/web/setup#available-libraries

// Your web app's Firebase configuration
// For Firebase JS SDK v7.20.0 and later, measurementId is optional
const firebaseConfig = {
  apiKey: "***",
  authDomain: "myapp.firebaseapp.com",
  databaseURL: "https://myapp-default-rtdb.europe-west1.firebasedatabase.app",
  projectId: "myappurl",
  storageBucket: "myappurl.appspot.com",
  messagingSenderId: "267227785525",
  appId: "***",
  measurementId: "G-K5CP51FRXR"
};

// Initialize Firebase
const app = initializeApp(firebaseConfig);
const analytics = getAnalytics(app);

But i get the error:

SyntaxError: Cannot use import statement outside a module linea:

It looks like i am not declaring it as a module. I have checked for tutorials and help but only find the how-to for Node.js, Google Script is not talked about.

Note: I have installed 'FirebaseApp' library.

Anyone knows how to do what i need? Is that even possible to use SDK v9 on GAS?

LeSmoox
  • 37
  • 6
  • 2
    What do you want to do exactly with Firebase? You should be able to connect to Firebase with your script by using the corresponding [library](https://sites.google.com/site/scriptsexamples/new-connectors-to-google-services/firebase), without having to install anything else. – Iamblichus Sep 28 '22 at 11:01
  • I am using Firebase as a database and this part is ok. But now i want to integrate Google Authentication to my app, and what i have to do it is not clear at all being on Google App Script. This one: https://firebase.google.com/docs/auth/web/manage-users and this one: https://firebase.google.com/docs/auth/web/google-signin It is kind of messy to me. I get this error when following the 'Web version 8 (namespaced): ReferenceError: firebase is not defined (riga 49, file "doGet") – LeSmoox Sep 28 '22 at 12:35
  • You cannot use the client-side JavaScript SDK for Firebase in your server-side Apps Script code like that. If you want to use Firebase in a web app that you create with Apps Script, make sure to include the client-side JavaScript into the HTML.JavaScript of the app - not in the server-side Apps Script code. – Frank van Puffelen Sep 28 '22 at 13:24
  • @FrankvanPuffelen sorry i don't really get the meaning. Could you elaborate a bit more? "make sure to include the client-side JavaScript into the HTML.": client-side javascript is included via [!= include("page-js"); ?>], is that what you mean? I was looking for some kind of tutorial for my case because i am missing the fundamentals here. – LeSmoox Sep 28 '22 at 13:54
  • Could you remove (or mask) the sensitive information like `apiKey`, etc, from the description? – Anindya Dey Sep 30 '22 at 14:45

1 Answers1

2

While Google Apps Script and Node.js uses JavaScript as programming language, Node.js has several features that aren't included in Google Apps Script, one of them is the the npm packages handling.

One option might be to use the npm package "as a client-side library". Please start by reading https://developers.google.com/apps-script/guides/web, then checkout Use npm package on client side.

Some client-side libraries might work as server side JavaScript. Nowadays Google Apps Script has two runtimes, but one, Rhino, depends on the old Google Apps Script IDE that will be retired soon. To learn about the new runtime, V8, please read https://developers.google.com/apps-script/guides/v8-runtime.

Some npm packages have being "converted" into Google Apps Script libraries. You might try to find on by searching this site, a service like GitHub, etc. Another option is to ask for a software recommendation on https://softwarerecs.stackexchange.com.

Another option is to use the Firebase REST API directly in Google Apps Script by using the Url Fetch Service.

Related

Rubén
  • 34,714
  • 9
  • 70
  • 166
  • OK i got it now. Thanks a lot for the explanation and for the documentation. I'll try to adjust it. – LeSmoox Sep 28 '22 at 18:50