/功能(目录)
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.
答案 0 :(得分:0)
如果删除导入正常工作,则导入文件时将无法部署。
缺少一些tsconfig选项:
"esModuleInterop": true,
"resolveJsonModule": true,
答案 1 :(得分:0)
您应该使用require()
(而不是import
)来提取JSON文件的内容。如果您确实必须使用import
(我不推荐),请使用read this。