'How to use `firebase-admin` inside firebase functions using ES2015

Nothing I do here seems to work, I can't find an example anywhere that actually seems correct

This:

import { initializeApp } from 'firebase-admin/app';

Throws:

Error [ERR_MODULE_NOT_FOUND]: Cannot find module '\path\to\my-proj\functions\node_modules\firebase-admin\app' imported from \path\to\my-proj\functions\index.js

This:

import * as admin from "firebase-admin";
admin.initializeApp();

Throws:

TypeError: admin.initializeApp is not a function

my functions' package.json looks like:

  "dependencies": {
    "firebase-admin": "^9.12.0",
    "firebase-functions": "^3.20.1"
  },

What's going on, what's the correct syntax?



Solution 1:[1]

You're using code samples meant for version 10 of firebase-admin, whereas you have version 9 installed. Read this for more information and run npm install firebase-admin@latest to get the latest version 10.1.0.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Doug Stevenson