Search code examples
node.jstypescriptfirebasegoogle-cloud-functionsfirebase-admin

Firebase Functions emulator Error: Cannot find module '../service-account.json'


I am trying to execute test functions with the firebase emulators:exec, the emulators start up successfully but are unable to load the functions code because of the following error

Error: Cannot find module '../service-account.json'

I have gone through npm install, npm run-script lint, npm run-script build etc many times, also wiped my node_modules folder and run npm install again, as well as restarting vscode, but I am repeatedly getting the error.

I have a functions folder in which the service-account.json is located:

├─ firebase.json
├─ firestore.indexes.json
├─ firestore.rules
├─ functions
│  ├─ .eslintrc.json
│  ├─ .gitignore
│  ├─ firestore-debug.log
│  ├─ lib
│  ├─ node_modules
│  ├─ package-lock.json
│  ├─ package.json
│  ├─ pubsub-debug.log
│  ├─ src
│  │  ├─ controller
│  │  │  ├─ functions
│  │  │  └─ shared.ts
│  │  ├─ index.ts
│  │  ├─ model
│  │  ├─ play-billing
│  │  ├─ service-account-firebase.json
│  │  ├─ service-account.json
│  │  └─ typings.d.ts
│  ├─ test
│  ├─ tsconfig.dev.json
│  └─ tsconfig.json
├─ gradle

The service-account.json should be accessed from the shared.ts file:

import * as functions from "firebase-functions";
import { PlayBilling } from "../play-billing";

import * as serviceAccountPlay from "../service-account.json";
import { InstanceIdManager } from "../model/InstanceIdManager";
import { ContentManager } from "../model/ContentManager";

/*
 * This file defines shared resources that are used in functions
 */

// Shared config
export const PACKAGE_NAME = functions.config().app.package_name;
...

I have double checked the path to the file is correct by hovering my mouse over it and it shows the full path correctly. I have checked my dependencies and everything seems to be in place:

├── @types/chai@4.2.15
├── @types/mocha@8.2.1
├── @types/node@14.14.31
├── @typescript-eslint/eslint-plugin@4.15.1
├── @typescript-eslint/parser@4.15.1
├── chai@4.3.0
├── copyfiles@2.4.1
├── eslint-plugin-import@2.22.1
├── eslint-plugin-promise@4.3.1
├── eslint@7.20.0
├── express@4.17.1
├── firebase-admin@9.5.0
├── firebase-functions-test@0.2.3
├── firebase-functions@3.13.1
├── googleapis@67.1.0
├── mocha@8.3.0
├── ts-node@9.1.1
└── typescript@4.1.5

I particularly puzzled because I have exactly the same structure, dependencies & functions code in another project, where it is working fine!

What am I missing? I have searched through other answers and can't find anything that I haven't already checked.

Note that the service-account.json is the exact same one that I am using in the other project where I am able to emulate the test functions without issue.

UPDATE After deleting my lib folder and rebuilding it, I now have a different error with the emulators loading my functions code:

It looks like you're trying to access functions.config().app but there is no value there.

My tsconfig.json is now showing an error on the opening brace:

Cannot find type definition file for 'express-serve-static-core 2'

but I cannot see anything wrong.

{
  "compilerOptions": {
    "lib": [
      "es6",
      "ESNext.AsyncIterable"
    ],
    "module": "commonjs",
    "noImplicitReturns": true,
    //"noUnusedLocals": true,
    "outDir": "lib",
    "sourceMap": true,
    //"strict": true,
    "target": "es6",
    "resolveJsonModule": true,
    "esModuleInterop": true
  },
  "compileOnSave": true,
  "include": [
    "src",
    "test",
    "./typings.d.ts",
  ]
}

my lib file structure is as follows:

.
├── src
│   ├── controller
│   ├── index.js
│   ├── index.js.map
│   ├── model
│   ├── play-billing
│   ├── service-account-firebase.json
│   └── service-account.json
└── test
    └── play-billing

Solution

  • I eventually fixed all of my problems as follows:

    Firstly, I deleted the entire lib folder, and then ran npm run-script build to build a new lib folder.

    This fixed the error for the emulators to find the module '...service-account.json'

    I had an error with finding index.js, which I fixed by correcting an error in the package.json file, for the path "main" to index.js

    I still could not run my functions from the emulators - I found the solution was to run the following command in the functions directory of my project.

    firebase functions:config:get > .runtimeconfig.json
    

    (see this answer here: https://stackoverflow.com/a/42978220/15046746)

    Now when I run firebase emulators:start, all of my functions are correctly initialised, and also I can run emulators:exec to run all of the test functions in the emulators.