升级到Firebase JS 8.0.0:尝试导入错误:未从"firebase/app"(导入为"firebase")导出"app" [英] Upgrade to Firebase JS 8.0.0: Attempted import error: 'app' is not exported from 'firebase/app' (imported as 'firebase')

查看:41
本文介绍了升级到Firebase JS 8.0.0:尝试导入错误:未从"firebase/app"(导入为"firebase")导出"app"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

After upgrading to 8.0.0, I get the following error:

尝试导入错误:未从"firebase/app"(导入为"firebase")导出"initializeApp".

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

我的导入看起来像这样:

My import looks like this:

import * as firebase from "firebase/app"
firebase.initializeApp({ ... })

TypeScript也抱怨:

TypeScript also complains:

属性'initializeApp'在类型'typeof import("/path/to/my/file")'上不存在.ts(2339)

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

我该如何解决?

推荐答案

在版本

In version 8.0.0, the Firebase SDK had a breaking change in the way it handles exports:

重大更改:package.json文件中的 browser 字段现在指向ESM捆绑包,而不是CJS捆绑包.使用ESM导入的用户必须现在使用默认导入而不是名称空间导入.

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.

8.0.0之前

import * as firebase from 'firebase/app'

8.0.0之后

import firebase from 'firebase/app'

使用 require('firebase/app') require('firebase')的代码将仍然可以使用,但是为了获得正确的输入(用于代码完成,例如)用户应将这些要求更改为 require('firebase/app').default require('firebase').default .这是因为SDK现在使用ESM捆绑软件的类型,而不同的捆绑软件共享一个打字文件.

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捆绑包默认导出:

So, you will have to use the new ESM bundle default export:

import firebase from "firebase/app"
firebase.initializeApp({ ... })

这篇关于升级到Firebase JS 8.0.0:尝试导入错误:未从"firebase/app"(导入为"firebase")导出"app"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆