Showing the Firebase error whenever I run the function locally using emulator in CLI
$ firebase emulators:start --only functions
Starting emulators: ["functions"]
functions: Using node@8 from host.
functions: Emulator started at http://localhost:5001
functions: Watching "E:\dir\functions" for Cloud Functions...
Error: Cannot find module 'E:\dir\functions'
at Function.Module._resolveFilename (module.js:548:15)
at Function.Module._load (module.js:475:25)
at Module.require (module.js:597:17)
at require (internal/module.js:11:18)
at C:\Users\d\AppData\Roaming\npm\node_modules\firebase-tools\lib\emulator\functionsEmulatorRuntime.js:459:29
at Generator.next ()
at C:\Users\d\AppData\Roaming\npm\node_modules\firebase-tools\lib\emulator\functionsEmulatorRuntime.js:7:71
at new Promise ()
at __awaiter (C:\Users\d\AppData\Roaming\npm\node_modules\firebase-tools\lib\emulator\functionsEmulatorRuntime.js:3:12)
at main (C:\Users\d\AppData\Roaming\npm\node_modules\firebase-tools\lib\emulator\functionsEmulatorRuntime.js:421:12)
Your function was killed because it raised an unhandled error.
I use typescript to write cloud functions.
here is my index.ts
import * as functions from 'firebase-functions';
import * as admin from "firebase-admin";
var cert = require("./skey.json");
admin.initializeApp({
credential: admin.credential.cert(cert),
databaseURL: "https://bhau-tk.firebaseio.com"
});
exports.basicHTTP = functions.https.onRequest((req, res) => {
res.send("Hello world!!");
})
package.json contains
{
"name": "functions",
"scripts": {
"lint": "tslint --project tsconfig.json",
"build": "tsc",
"serve": "npm run build && firebase serve --only functions",
"shell": "npm run build && firebase functions:shell",
"start": "npm run shell",
"deploy": "firebase deploy --only functions",
"logs": "firebase functions:log"
},
"engines": {
"node": "8"
},
"main": "lib/index.js",
"dependencies": {
"firebase-admin": "~7.0.0",
"firebase-functions": "^2.3.0",
"firebase-functions-test": "^0.1.6"
},
"devDependencies": {
"tslint": "^5.12.0",
"typescript": "^3.2.2"
},
"private": true
}
and project structure is
You should keep in mind that your project configuration is compiling TS sources files under src and putting the resulting JavaScript files in lib. So, you should refer to things in your functions folder relative to that location.
Since your compiled index.js is in lib, and your skey.json file is under src, you will have to refer to it that way:
var cert = require("../src/skey.json");
However, I wouldn't do it that way. I'd put skey.json in the functions folder (since it's not source code), and refer to it like this:
var cert = require("../skey.json");