ほとんどのFirebaseWebアプリでは、npmを介してSDKバージョン9を使用することを強くお勧めします。ただし、特別な要件を持つユーザーのために、FirebaseはSDKを追加するための代替方法を提供します。このページでは、これらの代替方法の詳細なセットアップ手順について説明します。
- CDN(コンテンツ配信ネットワーク)
- Node.jsアプリのnpm
これらの方法を使用して、バージョン9で利用可能なライブラリをアプリに追加できます。
CDNから
Firebase JavaScript SDKの部分的なインポートを設定し、必要なFirebase製品のみを読み込むことができます。 Firebaseは、Firebase JavaScript SDKの各ライブラリをグローバルCDN(コンテンツ配信ネットワーク)に保存します。
特定のFirebase製品(AuthenticationやCloud Firestoreなど)のみを含めるには、Firebaseサービスを使用する前に、
<body>
タグの下部に次のスクリプトを追加します。<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/9.15.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/9.15.0/firebase-analytics.js' // Add Firebase products that you want to use import { getAuth } from 'https://www.gstatic.com/firebasejs/9.15.0/firebase-auth.js' import { getFirestore } from 'https://www.gstatic.com/firebasejs/9.15.0/firebase-firestore.js' </script> </body>
Firebase構成オブジェクトを追加してから、アプリでFirebaseを初期化します。
<body> <script type="module"> // ... // TODO: Replace the following with your app's Firebase project configuration const firebaseConfig = { // ... }; // Initialize Firebase const app = initializeApp(firebaseConfig); </script> </body>
Node.jsアプリ
Firebase JavaScriptSDKをインストールします。
package.json
ファイルがまだない場合は、JavaScriptプロジェクトのルートから次のコマンドを実行して作成します。npm init
firebase
npmパッケージをインストールし、次のコマンドを実行してpackage.json
ファイルに保存します。npm install --save firebase@9.15.0
アプリでFirebaseモジュールを使用するには、次のいずれかのオプションを使用します。
任意のJavaScriptファイルからモジュールを
require
できます特定のFirebase製品(認証やクラウド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");
ES2015を使用してモジュールを
import
できます特定のFirebase製品(認証やクラウド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 project configuration const firebaseConfig = { //... }; // Initialize Firebase const app = initializeApp(firebaseConfig);