Cannot find module 'firebase-admin/app' - How to fix?

Viewed 611

I'm using Typescript with firebase-admin for firebase cloud function and I get "Cannot find module 'firebase-admin/app'" when compiling the code with TS. Already tried:

  • Re-Install Dependency
  • Delete node_modules

My Import:

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

I've also tried changing the import to the following. Then TS is satisfied, however Firebase Cloud function throws an error "cannot find module 'firebase-admin/app'"

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

My Package.json

{
  "name": "functions",
  "scripts": {
    "build": "tsc",
    "serve": "npm run build && firebase emulators:start --only functions",
    "shell": "npm run build && firebase functions:shell",
    "start": "npm run shell",
    "deploy": "firebase deploy --only functions",
    "logs": "firebase functions:log"
  },
  "engines": {
    "node": "16"
  },
  "main": "lib/index.js",
  "dependencies": {
    "firebase-admin": "^10.0.2",
    "firebase-functions": "^3.14.1",
    "moment": "^2.29.1",
    "node-fetch": "2.6.7"
  },
  "devDependencies": {
    "firebase-functions-test": "^0.2.0",
    "typescript": "^3.8.0"
  },
  "private": true
}

This is my tsconfig.json.

{
  "compilerOptions": {
    "module": "commonjs",
    "noImplicitReturns": true,
    "noUnusedLocals": false,
    "outDir": "lib",
    "sourceMap": true,
    "strict": false,
    "target": "es2017"
  },
  "compileOnSave": true,
  "include": [
    "src"
  ]
}

1 Answers

You are using "firebase-admin": "^10.0.2" which uses exports in module package.json to define the entry points. This won't work with eslint-plugin-import. Use the below line at the start of the file and try again.

// eslint-disable-next-line import/no-unresolved
Related