javascript - 升级到 Firebase JS 8.0.0 : Attempted import error: 'app' is not exported from 'firebase/app' (imported as 'firebase' )

标签 javascript typescript firebase

升级到 8.0.0 后,出现以下错误:

Attempted import error: 'initializeApp' is not exported from 'firebase/app' (imported as 'firebase').


我的导入如下所示:
import * as firebase from "firebase/app"
firebase.initializeApp({ ... })
TypeScript 还提示:

Property 'initializeApp' does not exist on type 'typeof import("/path/to/my/file")'. ts(2339)


我该如何解决?

最佳答案

在版本 8.0.0 ,Firebase SDK 在处理导出的方式上发生了重大变化:

Breaking change: browser fields in package.json files now point to ESM bundles instead of CJS bundles. Users who are using ESM imports must now use the default import instead of a namespace import.

Before 8.0.0

import * as firebase from 'firebase/app'

After 8.0.0

import firebase from 'firebase/app'

Code that uses require('firebase/app') or require('firebase') will still work, but in order to get proper typings (for code completion, for example) users should change these require calls to require('firebase/app').default or require('firebase').default. This is because the SDK now uses typings for the ESM bundle, and the different bundles share one typings file.


因此,您将不得不使用新的 ESM 捆绑包默认导出:
import firebase from "firebase/app"
firebase.initializeApp({ ... })
如果您正在使用 SDK version 9.0 ,改为阅读这个问题:
  • How do I fix a Firebase 9.0 import error? "Attempted import error: 'firebase/app' does not contain a default export (imported as 'firebase')."
  • 关于javascript - 升级到 Firebase JS 8.0.0 : Attempted import error: 'app' is not exported from 'firebase/app' (imported as 'firebase' ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64545862/

    相关文章:

    javascript - jQuery Live 可以在 Safari 上运行吗?

    javascript - 重新连接网络摄像头时,WebRTC 继续视频流

    将库函数导入 Web Worker 时 Angular 编译失败?

    Typescript 类型 a 不可分配给类型 b

    swift - 无法将类型 '(User?, Error?) -> ()' 的值转换为预期的参数类型 'AuthDataResultCallback?'

    javascript - 重构 Lodash 代码

    javascript - 类型 'xxx' 上不存在属性 '{}'

    javascript - Google Cloud Functions header 响应

    Firebase 数据验证

    javascript - 使用默认凭据访问 Google 表格