Ginpei
Ginpei

Reputation: 3178

Cannot find module 'firebase-admin' from 'index.cjs.js'

I was using @firebase/testing on Jest, but since it's deprecated and instructed to use new one, I decided to move to @firebase/rules-unit-testing.

Here is the code where I just switched them.

import { initializeAdminApp } from "@firebase/rules-unit-testing";
import "babel-polyfill";

it("is ok", async () => {
  const admin = initializeAdminApp({ projectId: "my-project" });

  try {
    const doc = admin.firestore().collection("items").doc("item-1");
    await doc.set({ name: "Item 1" });
    const ss = await doc.get();
    expect(ss.data()?.name).toBe("Item 1");
  } finally {
    await admin.delete();
  }
});

When I run this test with emu, it results "Cannot find module 'firebase-admin' from 'index.cjs.js'" error.

Cannot find module 'firebase-admin' from 'index.cjs.js'                      
                                                                                              
      3 |                                       
      4 | it("is ok", async () => {                                                              
    > 5 |   const admin = initializeAdminApp({ projectId: "my-project" });                           
        |                 ^

It passes if it is the old @firebase/testing.

What did I miss?

Upvotes: 1

Views: 1944

Answers (2)

Helder Silva
Helder Silva

Reputation: 11

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/*"
      ],
    }
  }
}

Upvotes: 1

Ginpei
Ginpei

Reputation: 3178

The firebase-admin is an npm package. Just installed it and all worked.

$ npm install -D firebase-admin

Upvotes: 4

Related Questions