1
- [ ![ Build Status ] ( https://travis-ci.org/firebase/firebase-js-sdk.svg?branch=master )] ( https://travis-ci.org/firebase/firebase-js-sdk )
1
+ <!-- TODO: Build/Test badges when available. -->
2
2
3
3
# Firebase - App success made simple
4
4
@@ -36,25 +36,41 @@ you should use the
36
36
37
37
## Get the code (browser)
38
38
39
- ### Script include
40
- > This brings in all Firebase features. See
41
- > [ "Include only the features you need"] ( #include-only-the-features-you-need )
42
- > below for
43
- > how to minimize download size by only including the scripts you need.
39
+ We recommend only installing the features you need. The individually installable services are:
44
40
45
- Include Firebase in your web application via a ` <script> ` tag:
41
+ - ` firebase-app ` - The core ` firebase ` client (required).
42
+ - ` firebase-app-check ` - Firebase App Check (optional).
43
+ - ` firebase-analytics ` - Firebase Analytics (optional).
44
+ - ` firebase-auth ` - Firebase Authentication (optional).
45
+ - ` firebase-database ` - The Firebase Realtime Database (optional).
46
+ - ` firebase-firestore ` - Cloud Firestore (optional).
47
+ - ` firebase-storage ` - Firebase Storage (optional).
48
+ - ` firebase-messaging ` - Firebase Cloud Messaging (optional).
49
+ - ` firebase-functions ` - Firebase Cloud Functions (optional).
50
+ - ` firebase-remote-config ` - Firebase Remote Config (optional).
51
+ - ` firebase-performance ` - Firebase Performance (optional).
52
+
53
+ ### Script include
54
+ Include Firebase in your web application via ` <script> ` tags. Create a script tag for each of the individual services you use (include ` firebase-app `
55
+ first):
46
56
47
57
``` html
48
- <script src =" https://www.gstatic.com/firebasejs/${JSCORE_VERSION}/firebase.js" ></script >
58
+ <!-- Always required. -->
59
+ <script src =" https://www.gstatic.com/firebasejs/${FIREBASE_VERSION}/firebase-app.js" ></script >
60
+ <!-- Include only the services you use, for example auth and database below. -->
61
+ <script src =" https://www.gstatic.com/firebasejs/${FIREBASE_VERSION}/firebase-auth.js" ></script >
62
+ <script src =" https://www.gstatic.com/firebasejs/${FIREBASE_VERSION}/firebase-database.js" ></script >
63
+ <!-- See above list for names of the other services. -->
49
64
50
65
<script >
51
- var app = firebase .initializeApp ({
66
+ const app = firebase .initializeApp ({
52
67
apiKey: ' <your-api-key>' ,
53
68
authDomain: ' <your-auth-domain>' ,
54
69
databaseURL: ' <your-database-url>' ,
55
70
projectId: ' <your-cloud-firestore-project>' ,
56
71
storageBucket: ' <your-storage-bucket>' ,
57
- messagingSenderId: ' <your-sender-id>'
72
+ messagingSenderId: ' <your-sender-id>' ,
73
+ appId: ' <your-app-id>'
58
74
});
59
75
// ...
60
76
</script >
@@ -64,100 +80,76 @@ _Note: To get a filled in version of the above code snippet, go to the
64
80
[ Firebase console] ( https://console.firebase.google.com/ ) for your app and click on "Add
65
81
Firebase to your web app"._
66
82
67
- ### npm bundler (Browserify, Webpack, etc.)
83
+ #### Alternative - all-in-one import
68
84
69
- > This brings in all Firebase features. See
70
- > [ "Include only the features you need"] ( #include-only-the-features-you-need )
71
- > below for
72
- > how to minimize bundle size by only importing the features you need.
85
+ > This brings in all Firebase features. We recommend the method above to
86
+ > minimize download size by only including the scripts you need.
87
+
88
+ Include Firebase in your web application via a ` <script> ` tag:
89
+
90
+ ``` html
91
+ <script src =" https://www.gstatic.com/firebasejs/${JSCORE_VERSION}/firebase.js" ></script >
73
92
74
- The Firebase JavaScript npm package contains code that can be run in the browser
75
- after combining the modules you use with a package bundler (e.g.,
76
- [ Browserify] ( http://browserify.org/ ) , [ Webpack] ( https://webpack.github.io/ ) ).
93
+ <script >
94
+ const app = firebase .initializeApp ({ ... });
95
+ // ...
96
+ </script >
97
+ ```
77
98
78
- Install the Firebase npm module:
99
+ ### NPM Bundler (Browserify, Webpack, Rollup, etc.)
79
100
101
+ Install the Firebase NPM module:
80
102
```
81
103
$ npm init
82
104
$ npm install --save firebase
83
105
```
84
106
85
- In your code, you can access Firebase using :
107
+ In your code, you can import the services you use :
86
108
87
109
``` js
88
- var firebase = require (' firebase' );
89
- var app = firebase .initializeApp ({ ... });
90
- ```
91
-
92
- If you are using ES6 imports or TypeScript:
110
+ // This import loads the firebase namespace along with all its type information.
111
+ import firebase from ' firebase/app' ;
93
112
94
- ``` js
95
- import firebase from ' firebase' ;
96
- var app = firebase . initializeApp ({ ... }) ;
113
+ // These imports load individual services into the firebase namespace.
114
+ import ' firebase/auth ' ;
115
+ import ' firebase/database ' ;
97
116
```
98
117
99
- ### Include only the features you need
118
+ Or if using ` require() ` :
100
119
101
- The full Firebase JavaScript client includes support for Firebase Authentication, the
102
- Firebase Realtime Database, Firebase Storage, and Firebase Cloud Messaging. Including
103
- code via the above snippets will pull in all of these features.
120
+ _ Use the ` .default ` import from ` firebase/app ` in order for
121
+ typings to work correctly.
122
+ See [ release notes for 8.0.0 ] ( https://firebase.google.com/support/release-notes/js#version_800_-_october_26_2020 ) . _
104
123
105
- You can reduce the amount of code your app uses by just including the features
106
- you need. The individually installable services are:
107
-
108
- - ` firebase-app ` - The core ` firebase ` client (required).
109
- - ` firebase-auth ` - Firebase Authentication (optional).
110
- - ` firebase-database ` - The Firebase Realtime Database (optional).
111
- - ` firebase-firestore ` - Cloud Firestore (optional).
112
- - ` firebase-storage ` - Firebase Storage (optional).
113
- - ` firebase-messaging ` - Firebase Cloud Messaging (optional).
114
- - ` firebase-functions ` - Firebase Cloud Functions (optional).
124
+ ``` js
125
+ const firebase = require (' firebase/app' ).default ;
126
+ require (' firebase/auth' );
127
+ require (' firebase/database' );
115
128
116
- From the CDN, include the individual services you use (include ` firebase-app `
117
- first):
129
+ const app = firebase . initializeApp ({ ... });
130
+ ```
118
131
119
- ``` html
120
- <script src =" https://www.gstatic.com/firebasejs/${FIREBASE_VERSION}/firebase-app.js" ></script >
121
- <script src =" https://www.gstatic.com/firebasejs/${FIREBASE_VERSION}/firebase-auth.js" ></script >
122
- <script src =" https://www.gstatic.com/firebasejs/${FIREBASE_VERSION}/firebase-database.js" ></script >
123
- <script src =" https://www.gstatic.com/firebasejs/${FIREBASE_VERSION}/firebase-firestore.js" ></script >
124
- <script src =" https://www.gstatic.com/firebasejs/${FIREBASE_VERSION}/firebase-storage.js" ></script >
125
- <script src =" https://www.gstatic.com/firebasejs/${FIREBASE_VERSION}/firebase-messaging.js" ></script >
126
- <script src =" https://www.gstatic.com/firebasejs/${FIREBASE_VERSION}/firebase-functions.js" ></script >
132
+ _ The type information from the import statement will include all of the SDKs,
133
+ not just the ones you have ` required ` , so you could get a runtime error if you
134
+ reference a non-required service._
127
135
128
- <script >
129
- var app = firebase .initializeApp ({ ... });
130
- // ...
131
- </script >
132
- ```
136
+ #### Alternative - all-in-one import
133
137
134
- When using the firebase npm package, you can ` require() ` just the services that
135
- you use:
138
+ > This brings in all Firebase features. We recommend the method above to
139
+ > minimize download size by only including the scripts you need.
136
140
137
141
``` js
138
- var firebase = require (' firebase/app' );
139
- require (' firebase/auth' );
140
- require (' firebase/database' );
141
-
142
- var app = firebase .initializeApp ({ ... });
142
+ // This import loads all Firebase services, whether used in your code or not.
143
+ import firebase from ' firebase' ;
143
144
```
144
145
145
- If you are using TypeScript with the npm package, you can import just the
146
- services you use:
146
+ Or with ` require() ` :
147
147
148
148
``` js
149
- // This import loads the firebase namespace along with all its type information.
150
- import firebase from ' firebase/app' ;
151
-
152
- // These imports load individual services into the firebase namespace.
153
- import ' firebase/auth' ;
154
- import ' firebase/database' ;
149
+ // This import loads all Firebase services, whether used in your code or not.
150
+ const firebase = require (' firebase' ).default ;
155
151
```
156
152
157
- _ The type information from the import statement will include all of the SDKs,
158
- not just the ones you have ` required ` , so you could get a runtime error if you
159
- reference a non-required service._
160
-
161
153
## Get the code (Node.js - server and command line)
162
154
163
155
### NPM
@@ -177,8 +169,10 @@ $ npm install --save firebase
177
169
In your code, you can access Firebase using:
178
170
179
171
``` js
180
- var firebase = require (' firebase' );
181
- var app = firebase .initializeApp ({ ... });
172
+ const firebase = require (' firebase/app' ).default ;
173
+ require (' firebase/auth' );
174
+ require (' firebase/database' );
175
+ const app = firebase .initializeApp ({ ... });
182
176
// ...
183
177
```
184
178
@@ -196,23 +190,6 @@ import 'firebase/database';
196
190
197
191
_ Known issue for typescript users with --experimental-modules: you have to set allowSyntheticDefaultImports to true in tsconfig.json to pass the type check. Use it with caution since it makes the assumption that all modules have a default export, which might not be the case for the other dependencies you have. And Your code will break if you try to import the default export from a module that doesn't have default export._
198
192
199
- Firebase Storage is not included in the server side Firebase npm module.
200
- Instead, you can use the
201
- [ ` google-cloud ` Node.js client] ( https://github.com/GoogleCloudPlatform/google-cloud-node ) .
202
-
203
- ```
204
- $ npm install --save google-cloud
205
- ```
206
-
207
- In your code, you can access your Storage bucket using:
208
-
209
- ``` js
210
- var gcloud = require (' google-cloud' )({ ... });
211
- var gcs = gcloud .storage ();
212
- var bucket = gcs .bucket (' <your-firebase-storage-bucket>' );
213
- ...
214
- ```
215
-
216
193
Firebase Cloud Messaging is not included in the server side Firebase npm module.
217
194
Instead, you can use the
218
195
[ Firebase Cloud Messaging Rest API] ( https://firebase.google.com/docs/cloud-messaging/send-message ) .
0 commit comments