對於大多數 Firebase 網頁應用程式,我們強烈建議使用透過 npm 取得的 SDK。不過,Firebase 也提供其他新增 SDK 的方式,可滿足特殊需求的使用者。 本頁提供下列替代方法的詳細設定說明:
- CDN (內容傳遞聯播網)
- Node.js 應用程式的 npm
您可以使用這些方法,將任何可用程式庫新增至應用程式。
從 CDN
您可以設定部分匯入 Firebase JavaScript SDK,只載入所需的 Firebase 產品。Firebase 會將 Firebase JavaScript SDK 的每個程式庫儲存在全球 CDN (內容傳遞網路) 中。
- 如要只加入特定 Firebase 產品 (例如 Authentication 和 Cloud Firestore),請將下列指令碼新增至 - <body>標記的最下方,但請先使用任何 Firebase 服務:- <body> <!-- Insert this script at the bottom of the HTML, but before you use any Firebase services --> <script type="module"> import { initializeApp } from 'https://www.gstatic.com/firebasejs/12.5.0/firebase-app.js' // If you enabled Analytics in your project, add the Firebase SDK for Google Analytics import { getAnalytics } from 'https://www.gstatic.com/firebasejs/12.5.0/firebase-analytics.js' // Add Firebase products that you want to use import { getAuth } from 'https://www.gstatic.com/firebasejs/12.5.0/firebase-auth.js' import { getFirestore } from 'https://www.gstatic.com/firebasejs/12.5.0/firebase-firestore.js' </script> </body> 
- 新增 Firebase 設定物件,然後在應用程式中初始化 Firebase: - <body> <script type="module"> // ... // TODO: Replace the following with your app's Firebase configuration const firebaseConfig = { // ... }; // Initialize Firebase const app = initializeApp(firebaseConfig); </script> </body> 
Node.js 應用程式
- 安裝 Firebase JavaScript SDK: - 如果您尚未建立 - package.json檔案,請從 JavaScript 專案的根目錄執行下列指令來建立:- npm init 
- 執行下列指令,安裝 - firebasenpm 套件並儲存至- package.json檔案:- npm install --save firebase@12.5.0 
 
- 如要在應用程式中使用 Firebase 模組,請採取下列其中一種做法: - 您可以從任何 JavaScript 檔案 - require模組- 如要只納入特定 Firebase 產品 (例如 Authentication 和 Cloud Firestore): - // Firebase App (the core Firebase SDK) is always required and // must be listed before other Firebase SDKs var firebase = require("firebase/app"); // Add the Firebase products that you want to use require("firebase/auth"); require("firebase/firestore");
- 您可以使用 ESM 語法 - import模組- 如要只納入特定 Firebase 產品 (例如 Authentication 和 Cloud Firestore): - // Firebase App (the core Firebase SDK) is always required and // must be listed before other Firebase SDKs import firebase from "firebase/app"; // Add the Firebase services that you want to use import "firebase/auth"; import "firebase/firestore";
 
- 新增 Firebase 設定物件,然後在應用程式中初始化 Firebase: - import { initializeApp } from 'firebase/app'; // TODO: Replace the following with your app's Firebase configuration const firebaseConfig = { //... }; // Initialize Firebase const app = initializeApp(firebaseConfig);