部署云功能时出错。找不到.json

问题描述 投票:1回答:2

我尝试导入.json凭据文件时无法部署我的函数。

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

/ functions(目录)

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
}

功能/ 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.
json typescript google-cloud-functions
2个回答
0
投票

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


0
投票

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

缺少一些tsconfig选项:

"esModuleInterop": true,
"resolveJsonModule": true,
© www.soinside.com 2019 - 2024. All rights reserved.