开发者问题收集

部署 Cloud Functions 时出错,未找到 .json

2018-09-06
1987

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

  1. 运行 firebase init 函数时,我选择了 TypeScript 选项。
    1. 然后我将函数放入 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
}

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个回答

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

Doug Stevenson
2018-09-06

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

缺少一些 tsconfig 选项:

"esModuleInterop": true,
"resolveJsonModule": true,
basarat
2018-09-06