Error in deploy Cloud Functions .json not found

Viewed 1533

I can not deploy my functions when I try to import a .json credential file.

  1. When running firebase init functions I selected the TypeScript option.
    1. Then I put my functions in TypeScript and tried loading the .json file with the credentials of my project in order to use firebase-admin.

/functions (Directory)

tsconfig.json

{
  "compilerOptions": {
    "lib": ["es6"],
    "module": "commonjs",
    "noImplicitReturns": true,
    "outDir": "lib",
    "sourceMap": true,
    "target": "es6",
    "types" : [ "node" ],
    "esModuleInterop": true,
    "resolveJsonModule": true,
  },
  "compileOnSave": true,
  "include": [
    "src",
    "./typings.d.ts"
  ]
}

typings.d.ts:

declare module "*.json" {
    const value: any;
    export default value;
  }

package.json:

{
  "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"
  },
  "main": "lib/index.js",
  "dependencies": {
    "firebase-admin": "~6.0.0",
    "firebase-functions": "^2.0.3"
  },
  "devDependencies": {
    "tslint": "~5.8.0",
    "typescript": "~2.8.3"
  },
  "private": true
}

functions/src/

-- serviceAccountKey.json

-- index.ts

index.ts:

import * as admin from 'firebase-admin';
import * as serviceAccount from './serviceAccountKey.json';

admin.initializeApp({
    credential: admin.credential.cert(serviceAccount),
    databaseURL: "DATABASE-URL"
});

If I remove the import works normally, I can not deploy when I import the file.

Error:

i  deploying functions
Running command: npm --prefix "functions" run lint

> functions@ lint Z:\functions
> tslint --project tsconfig.json

Running command: npm --prefix "functions" run build

> functions@ build Z:\functions
> tsc

tsconfig.json(11,5): error TS5023: Unknown compiler option 'resolveJsonModule'.
+  functions: Finished running predeploy script.
i  functions: ensuring necessary APIs are enabled...
+  functions: all necessary APIs are enabled
i  functions: preparing functions directory for uploading...

Error: Error parsing triggers: Cannot find module './serviceAccountKey.json'

Try running "npm install" in your functions directory before deploying.
2 Answers

If I remove the import works normally, I can not deploy when I import the file.

A few tsconfig options are missing:

"esModuleInterop": true,
"resolveJsonModule": true,

You should use require() (not import) to pull in the contents of a JSON file. If you really must use import (I don't recommend it), read this.

Related