找不到部署Cloud Functions .json的错误

时间:2018-09-06 01:33:05

标签: json typescript google-cloud-functions

当我尝试导入.json凭证文件时,我无法部署功能。

  1. 在运行firebase初始化函数时,我选择了TypeScript 选项。
    1. 然后我将函数放入TypeScript中并尝试加载.json 文件与我的项目的凭据,以便使用 firebase-admin。

/功能(目录)

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"
});

如果删除导入正常工作,则导入文件时将无法部署。

错误:

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 个答案:

答案 0 :(得分:0)

  

如果删除导入正常工作,则导入文件时将无法部署。

缺少一些tsconfig选项:

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

答案 1 :(得分:0)

您应该使用require()(而不是import)来提取JSON文件的内容。如果您确实必须使用import(我不推荐),请使用read this