当我部署安装了模块 firebase
的云功能时,我收到以下错误(下图)。
我已经尝试安装 @types/firebase
和 firebase
并收到相同的错误。我很确定这个错误与这个模块有关,因为当 firebase
和 @types/firebase
都被卸载时,函数会正确部署。
另外,云函数还没有调用这个模块,所以我认为错误不在函数中。我已经为我的 package.json
文件和 ts.config
文件提供了代码。知道为什么我会收到此错误以及如何在不收到此错误的情况下导入模块吗?让我知道是否还有其他文件值得审查。谢谢。
./package.json
{
"name": "functions",
"description": "Cloud Functions for Firebase",
"scripts": {
"lint": "./node_modules/.bin/tslint -p tslint.json",
"serve": "firebase serve --only functions",
"shell": "firebase functions:shell",
"start": "npm run shell",
"deploy": "firebase deploy --only functions",
"logs": "firebase functions:log",
"build": "./node_modules/.bin/tsc"
},
"dependencies": {
"dom": "0.0.3",
"expo-server-sdk": "^2.4.0",
"firebase-admin": "~5.12.1",
"firebase-functions": "^1.0.3",
"typescript": "^2.9.2"
},
"devDependencies": {
"eslint": "^4.12.0",
"eslint-plugin-promise": "^3.6.0",
"ts-loader": "^4.4.2"
},
"main": "lib/index.js",
"private": true
}
./tsconfig.json
{
"compilerOptions": {
"lib": ["es6"],
"module": "commonjs",
"noImplicitReturns": true,
"outDir": "lib",
"sourceMap": true,
"target": "es6",
"allowJs": true,
},
"compileOnSave": true,
"include": [
"src"
]
}
最佳答案
如果像我一样,您实际上需要在云函数中使用客户端 firebase 模块,问题和解决方法在 #880 中描述。 :
A workaround is to add
"dom"
to the"lib"
compiler option in yourtsconfig.json
Our packages assume that DOM types exist. The problem is that importing
firebase/app
imports typings of all packages.Some of our packages (like Messaging) only work in the browser. These packages (or their types) should not be imported in a Node environment.
编辑:这是发布在问题线程中的更安全的答案
The suggested/promoted workaround here of adding "dom" to compilerOptions.lib is NOT safe. That would be telling the compiler that any and all dom type references are valid in my code, which is clearly not the case since Node.js is not a browser environment. If this library is meant to be used as a Node.js TypeScript library, then it should not depend on ambient dom-specific type definitions. A safer workaround for TypeScript 3.0 users is to import firebase as follows:
/// <reference lib="dom" />
import * as firebase from 'firebase';
That first line is a "triple-slash lib directive" (https://www.typescriptlang.org/docs/handbook/triple-slash-directives.html) first available in TypeScript 3.0. It's like adding dom to compilerOptions.lib but only for that one file. Note that the triple-slash directive must appear before any statements or it gets interpreted as just a normal comment.
关于typescript - "Cannot find name ' ServiceWorkerRegistration 使用Typescript创建firebase云函数时出现'"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51073986/