firebase-admin-node: Cannot find module 'firebase-admin/app'

package.json requirements look like this –

  "dependencies": {
    "@sentry/node": "^5.30.0",
    "body-parser": "1.19.0",
    "express": "^4.17.1",
    "firebase-admin": "10.0.0",
    "mysql": "^2.18.1",
    "pug": "3.0.2"
  }

node_modules has the corresponding module in the path ~/node_modules/firebase-admin/lib/app

Node code reads as –

const initializeApp  = require('firebase-admin/app');

And when the app is run, the following stacktrace emerges.

 Error: Cannot find module 'firebase-admin/app'
     at Function.Module._resolveFilename (module.js:547:15)
     at Function.Module._load (module.js:474:25)
     at Module.require (module.js:596:17)
     at require (internal/module.js:11:18)
     at Object.<anonymous> (/var/www/html/project/backend/main.js:16:24)
     at Module._compile (module.js:652:30)
     at Object.Module._extensions..js (module.js:663:10)
     at Module.load (module.js:565:32)
     at tryModuleLoad (module.js:505:12)
     at Function.Module._load (module.js:497:3)

About this issue

  • Original URL
  • State: open
  • Created 3 years ago
  • Reactions: 33
  • Comments: 39 (10 by maintainers)

Most upvoted comments

Solved it by ignoring the import recommendation. Instead of

'use strict';

const { initializeApp } = require('firebase-admin/app');
const { getFirestore, Timestamp, GeoPoint } = require('firebase-admin/firestore');

exports.initializeApp = () => initializeApp();
exports.getFirestore = () => getFirestore();
exports.Timestamp = Timestamp;
exports.GeoPoint = GeoPoint;

I am now using what existed before v10:

'use strict';

const firebaseAdmin = require('firebase-admin');

exports.initializeApp = () => firebaseAdmin.initializeApp();
exports.getFirestore = () => firebaseAdmin.firestore();
exports.Timestamp = firebaseAdmin.firestore.Timestamp;
exports.GeoPoint = firebaseAdmin.firestore.GeoPoint;

Similarly to @HelderSi, I had to do a moduleNameMapper config, but I’m using yarn v2 PnP, so my config looks like this:

import { pathsToModuleNameMapper } from "ts-jest/utils";

export default {
  preset: "ts-jest",
  testEnvironment: "node",
  modulePaths: ["<rootDir>/src"],
  moduleNameMapper: pathsToModuleNameMapper({
    "firebase-admin/*": ["firebase-admin/lib/*"],
  }),
};

I started to use firebase-admin@^10.0.1 and I had this error when running jest tests. It couldn’t map “firebase-admin/app” to “firebase-admin/lib/app” as expected. So I have mapped this manually on jest.config.ts:

import { pathsToModuleNameMapper } from 'ts-jest/utils';
import { compilerOptions } from './tsconfig.json';

...
export default {
 ...
 moduleNameMapper: pathsToModuleNameMapper(
    {
      ...compilerOptions.paths,
      'firebase-admin/*': ['node_modules/firebase-admin/lib/*'],
    },
    {
      prefix: '<rootDir>',
    },
  ),
...
}

That worked for me.

And just to note, my tsconfig.json is like this:

{
  "compilerOptions": {
    ...
    "baseUrl": ".",
    "paths": {
      "modules/*": [
        "src/modules/*"
      ],
      "shared/*": [
        "src/shared/*"
      ],
    }
  }
}

any updates on this issue ? I am not able to use modular imports on Node 16 Cannot find module 'firebase-admin/auth' My installed version is 10.0.0

@DallasP9124 your import is wrong:

import { getAuth } from "firebase-admin/lib/auth"

Should be corrected as:

import { getAuth } from "firebase-admin/auth"

Same problem here. The suggested method in the documentation did not work.

However this solved it:

import * as admin from 'firebase-admin';

admin.initializeApp();

const db = admin.firestore();
const bucket = admin.storage().bucket();
const auth = admin.auth();

I started getting this after installing the eslint-plugin-node module and enabling the plugin. The rule node/no-missing-import was causing the error. I fixed it by adding allowModules: ["firebase-admin"] in the rule config, like this:

"node/no-missing-import": [
  "error",
    {
      allowModules: ["firebase-admin"],
    },
  ],

I’m also getting this error when using the snippet provided:

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

=> Error: Cannot find module 'firebase-admin/app'

Node v14.16.1

Confirming that using import firebaseAdmin from 'firebase-admin' resolves it, however that’s a big headache. For now I’ve given up on this approach and gone back to using import Firestore from '@google-cloud/firestore' (and it looks like firebase-admin just wraps that anyway so no harm I guess?)

For anyone else looking for things to try, learn from my mistake. When upgrading to firebase-admin@10, I accidentally made it a dev dependency, thus triggering this error.

Deleting node_modules, package-lock.json, and running npm cache clean --force fixed this for me on Node 16.14.

A co-worker had a similar issue, so just chiming in in case it helps someone. Basically, make sure you’re running tooling that supports package.json exports field.

firebase-admin/app isn’t a real file on disk, it’s mapped as per:

https://github.com/firebase/firebase-admin-node/blob/7b15b27f2cfe05200fae1f907f9048788ac42e4c/package.json#L108-L111

In our case we were seeing discrepancies between developers’ machines. Ensuring everyone was running Node 12.7 or greater resolved the issue.

I did have the same issue with not finding the module ‘firebase-admin/app’. Ended up accessing firebase products with suggestions from others at the top of this thread. Tried it again the default way from google docs , regardless of the error ‘module not found’ everything still works fine and able to use firebase product (firestore). You can IGNORE the error and run your code. I am node version 12

I got fixed by updating fireabse-admin and node version as below. firebase-admin: 10.0.0 node: v14.15.4

We encountered the issue in Vite during alpha testing. See #1340 (also https://github.com/vitejs/vite/issues/3953). It’s a bug in their module resolver. We cannot do much about that on our end, but it sounds like the PR you’ve linked above is expected to fix it.

Other platforms with known issues include Jest and ESLint (see #1481)

Folks who are seeing this error also please check their library version using one of the following methods:

Using npm ls

$ npm ls firebase-admin       
mods@1.0.0 
└── firebase-admin@10.0.0

Directly checking the package.json of the library

$ grep "version" node_modules/firebase-admin/package.json
  "version": "10.0.0"

If the version is indeed 10.x or higher, please share a complete repro that we can run.

Edit – I can confirm that downgrading to v12 did solve the issue.

Assuming there are no other libraries, frameworks or custom module loaders in play, more likely explanation is your Node 17 setup was somehow loading an old version of firebase-admin (e.g. v9), that didn’t have the new module exports. Changing to Node 12 pulled in the latest version of the library, and picked up the new entry points. Just a guess.