Version 10 of the Admin Node.js SDK introduces two important changes:
- Support for Node.js 10 is discontinued (this is a breaking change)
- The SDK has adopted a modular API pattern
This guide provides instructions and information to help developers upgrade existing Node.js apps from earlier versions of the Admin SDK to v10.
Update Node.js to v12 or later
With the Admin Node.js SDK v10 release, Firebase has discontinued support for Node.js 10. You must use Node.js 12 or later when using the Admin SDK. If you're using the Admin Node.js SDK together with Cloud Functions for Firebase, make sure that you've upgraded your Node.js version to 12 or later.
Use modules instead of namespaces
Since its inception, the Admin Node.js SDK has offered a stable API structured as a nested namespace hierarchy. As a result, you might have become familiar with writing code that looks like this:
// Import the global `admin` namespace
import * as admin from 'firebase-admin';
const app: admin.app.App = admin.initializeApp();
const token: string = await admin.auth().createCustomToken('alice');
const user: admin.auth.UserRecord = await admin.auth().getUser('bob');
As of v10, the Admin Node.js SDK offers multiple module entry points with named
exports. We recommend that you use these module entry points to access the
various APIs of the SDK, instead of using the global admin namespace.
Here's what the preceding example would look like with the module entry points:
TypeScript
// Import only what you need
import { initializeApp, App } from 'firebase-admin/app';
import { getAuth, UserRecord } from 'firebase-admin/auth';
const app: App = initializeApp();
const token: string = await getAuth().createCustomToken('alice');
const user: UserRecord = getAuth().getUser('bob');
Node.js
// Import only what you need
const { initializeApp } = require('firebase-admin/app');
const { getAuth } = require('firebase-admin/auth');
const app = initializeApp();
const token = await getAuth().createCustomToken('alice');
const user = getAuth().getUser('bob');
Use v10 modular entry points
In the preceding examples, you're no longer importing a global admin
namespace. Instead, you explicitly import only the symbols you need from several
module entry points. Also, you no longer have to use triple-nested type
identifiers like admin.auth.UserRecord and admin.database.Reference. Because
each type belongs to exactly one module, you can import them by their short
names like UserRecord and Reference.
Here are all the module entry points available in the SDK as of v10:
firebase-admin/appfirebase-admin/authfirebase-admin/databasefirebase-admin/firestorefirebase-admin/instance-idfirebase-admin/machine-learningfirebase-admin/messagingfirebase-admin/project-managementfirebase-admin/remote-configfirebase-admin/security-rulesfirebase-admin/storage
The following table shows the replacement import syntax for each of the legacy namespace functions:
| v9 | v10 |
|---|---|
admin.initializeApp() |
|
admin.app() |
|
admin.credential.cert() |
|
admin.auth() |
|
admin.database() |
|
admin.firestore() |
|
admin.instanceId() |
|
admin.machineLearning() |
|
admin.messaging() |
|
admin.projectManagement() |
|
admin.remoteConfig() |
|
admin.securityRules() |
|
admin.storage() |
|
Use exported functions instead of methods on App
In the legacy API, the App object exposed a number of methods like app.auth
and app.database. We recommend you avoid using these methods, and instead use
the same module entry points described previously to obtain service instances
scoped to a given App object, and perform other app-specific tasks.
| v9 | v10 |
|---|---|
app.auth() |
|
app.database() |
|
app.database(url) |
|
app.firestore() |
|
app.instanceId() |
|
app.machineLearning() |
|
app.messaging() |
|
app.projectManagement() |
|
app.remoteConfig() |
|
app.securityRules() |
|
app.storage() |
|
app.delete() |
|
ES modules support
Node.js 12 and later come with experimental support for ES modules, so you can
use the export and import keywords in your code even without TypeScript.
As of the v10 release, the Admin Node.js SDK also provides ES modules support,
so that if you implement ES modules on plain Node.js, you can import the SDK
using import syntax.
To use ES modules with the Admin SDK, first make sure you've enabled ESM
support for your Node.js runtime. This is usually done by adding a
"type": "module" field to your package.json file. Then you can write
application code that looks like this:
// With {type: module} in the package.json...
// Import only what you need
import { initializeApp } from 'firebase-admin/app';
import { getAuth } from 'firebase-admin/auth';
const app = initializeApp();
const token = await getAuth().createCustomToken('alice');
const user = getAuth().getUser('bob');