简体   繁体   中英

Install Firebase SDK v9 on Google App Script?

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM