node.jsjsontypescriptgoogle-cloud-functions

Cannot find module 'serviceAccountKey.json'


I'm trying to implement Firebase Functions and I'm stuck at importing my service account key from my project. This is my code.

import * as admin from 'firebase-admin';

var serviceAccount = require('./' + functions.config().environment.name + '/serviceAccountKey.json');

admin.initializeApp({
  credential: admin.credential.cert(serviceAccount),
  databaseURL: "<MYDATABASEURL>.firebaseio.com"
});

When I run firebase serve this is the output

Cannot find module './development/serviceAccountKey.json' at Function.Module._resolveFilename (internal/modules/cjs/loader.js:636:15) at Function.Module._load (internal/modules/cjs/loader.js:562:25) at Module.require (internal/modules/cjs/loader.js:690:17) at require (internal/modules/cjs/helpers.js:25:18) at Object. (D:\workspace\projects\backend\momentos-backend\functions\lib\index.js:29:22) at Module._compile (internal/modules/cjs/loader.js:776:30) at Object.Module._extensions..js (internal/modules/cjs/loader.js:787:10) at Module.load (internal/modules/cjs/loader.js:653:32) at tryModuleLoad (internal/modules/cjs/loader.js:593:12) at Function.Module._load (internal/modules/cjs/loader.js:585:3)

My Typescript version is 3.6.3 and these are my compiler options in my tsconfig.json

...

"compilerOptions": {
    "module": "commonjs",
    "noImplicitReturns": true,
    "noUnusedLocals": false,
    "outDir": "lib",
    "sourceMap": true,
    "strict": true,
    "target": "es2017",
    "resolveJsonModule": true,
  },

...

I don't know if I'm forgetting something but I think this should work. It would be very helpful if somebody could find me a solution for this. Thanks in advance!


Solution

  • Nevermind, I just noticed that require starts searching from node_modules dir. Bit of a newbie error right there.