Can't import firebase-admin in Node.js
Asked Answered
B

2

6

I have a Node (14.3.0) server where I enabled ES6 module imports in my package.json by adding the following line:

package.json:

"type": "module",

According to the firebase-admin docs here: https://firebase.google.com/docs/admin/setup/#node.js

If you are using ES2015, you can import the module instead:

import * as admin from 'firebase-admin';

When I use import * as admin from 'firebase-admin'; I get the following error:

credential: admin.credential.applicationDefault(),
TypeError: Cannot read property 'applicationDefault' of undefined

It seems that firebase-admin isn't imported properly - I have tried removing the "type": "module" line in package.json and importing firebase-admin with require:

const admin = require(firebase-admin)

and it works, so my question is - is it possible to import firebase-admin in Node using ES6, and if so, how?

Below is a complete, minimal, reproduction:

server.js

import express from 'express';
import * as admin from 'firebase-admin';

const app = express();
const PORT = process.env.PORT || 5000;

app.use(express.json());

admin.initializeApp({
  credential: admin.credential.applicationDefault(),
  databaseURL: process.env.FIREBASE_DB_URL,
});

app.listen(PORT, () => console.log(`listening on ${PORT}`));

export default app;

package.json

{
  "name": "server",
  "version": "1.0.0",
  "main": "index.js",
  "license": "MIT",
  "dependencies": {
    "express": "^4.17.1",
    "firebase-admin": "^9.2.0",
  },
  "type": "module",
  "scripts": {
    "dev": "node server.js"
  },
  "engines": {
    "node": "14.x"
  }
}

NOTE: Before running the server, make sure to do in your shell (Mac/Linux):

export GOOGLE_APPLICATION_CREDENTIALS="/your/path/to/service-account-file.json"

Biagio answered 10/10, 2020 at 17:40 Comment(2)
I import firebase-admin like this all the time. It might be helpful if you explain in more detail what a complete, minimal reproduction looks like that generates this error.Premonition
Hi @DougStevenson - I added this. Happy to augment the question if more clarification is needed. When I remove "type":"module" from the package.json, and use const x = require('package-name') instead of import, and remove default export app - the example works.Biagio
B
10

I asked this question on the firebase-admin-node github. Apparently, they hadn't tested imports with Node 14 yet. The answer is simply:

import admin from 'firebase-admin'

You can see an explanation here:

https://github.com/firebase/firebase-admin-node/issues/1061#event-3868300300

Biagio answered 13/10, 2020 at 17:1 Comment(2)
This does not work for me. Has something changed since this was answered?Chanticleer
this kept me wondering why I cannot use it at frontend nextjs app. It needs to be at the backend which use nodejsMadly
P
4

Use it like this:

import * as admin from 'firebase-admin';
const {credential} = admin;

this way you will be able to use the functions.

Pugilist answered 10/10, 2020 at 17:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.