简体   繁体   English

找不到模块:无法在 React JS 中解析“firebase”

[英]Module not found: Can't resolve 'firebase' in react js

In my reactjs project I have installed firebase using 'npm i firebase'.在我的 reactjs 项目中,我使用“npm i firebase”安装了 firebase。 And inside of src folder I have created firebase.js file for firebase config.在 src 文件夹中,我为 firebase 配置创建了 firebase.js 文件。

import firebase from 'firebase'

const firebaseConfig = {
  apiKey: "******",
  authDomain: "****",
  projectId: "***",
  storageBucket: "*****",
  messagingSenderId: "****",
  appId: "****"
};

firebase.initializeApp(config);

export default firebase

Even after installing firebase in my project.即使在我的项目中安装 firebase 之后。 It gives error:它给出了错误:

./src/firebase.js
Module not found: Can't resolve 'firebase' in '/home/rupak/Projects/web/firebasereact/src'

Can anyone find the problem?谁能找到问题所在?

You are not using the new Modular syntax.您没有使用新的模块化语法。 You can keep using existing code by changing the imports to compat version:您可以通过将导入更改为compat版本来继续使用现有代码:

import firebase from 'firebase/compat/app'
import 'firebase/firestore'
// import 'firebase/[SERVICE_NAME]'

However, I'd recommend upgrading to the new Modular SDK , which offers some benefits with tree-shaking.但是,我建议升级到新的Modular SDK ,它通过摇树优化提供了一些好处。 Checkout this Firecast for detailed information:查看此 Firecast 以获取详细信息:

Getting started with Firebase for Web入门 Firebase 为 Web

Refer Firebase docs if you are using newer version initializeApp is moved to firebase/app package如果您使用的是较新版本,请参阅Firebase文档 initializeApp 已移至 firebase/app package

Hey I had the same situation stated above... But by applying this:嘿,我遇到了与上述相同的情况......但是通过应用这个:

import firebase from 'firebase/compat/app'
import 'firebase/firestore'
// import 'firebase/[SERVICE_NAME]'
//export
export const auth = firebase.auth();
export const googleAuthProvider = new firebase.auth.GoogleAuthProvider();

I only removed the (/app) , and it worked... THANK YOU我只删除了(/app) ,它起作用了……谢谢

I have faced the same issue, It seems firebase 9.17.0 just dropped.我遇到了同样的问题,似乎 firebase 9.17.0 刚刚下降。

Rolling back to the 9.16.0 solved the problem.回滚到9.16.0解决了这个问题。

for yarn package:对于纱线 package:

yarn add firebase@9.16.0

for npm:对于 npm:

 npm i firebase@9.16.0

Now check your package.json file looks like this.现在检查您的package.json文件如下所示。

"firebase": "9.16.0",

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM