Reputation: 97
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/[email protected]
├── @types/[email protected]
├── @types/[email protected]
├── @typescript-eslint/[email protected]
├── @typescript-eslint/[email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
└── [email protected]
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
Upvotes: 2
Views: 1409
Reputation: 97
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.
Upvotes: 2
Reputation: 7438
If I'm reading this correct your service account file is inside the src/
directory. But once your function is built, the compiled code gets deployed to the lib/
directory. There your relative path is no longer valid.
Upvotes: 0