Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

`Firebase` package was successfully found. However, this package itself specifies a `main` module field that could not be resolved

I'm trying to read and write to firestore, use firebase's authentication, and firebase's storage within a expo managed react-native application.

Full Error:

While trying to resolve module `firebase` from file `C:\Users\joshu\Desktop\VSProjects\VolleyballConnect\firebase.js`, the package `C:\Users\joshu\Desktop\VSProjects\VolleyballConnect\node_modules\firebase\package.json` was successfully found. However, this package itself specifies a `main` module field that could not be resolved (`C:\Users\joshu\Desktop\VSProjects\VolleyballConnect\node_modules\firebase\index`. Indeed, none of these files exist:

  * C:\Users\joshu\Desktop\VSProjects\VolleyballConnect\node_modules\firebase\index(.native|.android.ts|.native.ts|.ts|.android.tsx|.native.tsx|.tsx|.android.js|.native.js|.js|.android.jsx|.native.jsx|.jsx|.android.json|.native.json|.json)
  * C:\Users\joshu\Desktop\VSProjects\VolleyballConnect\node_modules\firebase\index\index(.native|.android.ts|.native.ts|.ts|.android.tsx|.native.tsx|.tsx|.android.js|.native.js|.js|.android.jsx|.native.jsx|.jsx|.android.json|.native.json|.json)

My firebase config file:

import firebase from "firebase";
import { initializeApp } from "firebase/app";
import "firebase/firestore";
import "firebase/auth";
import "firebase/storage";

// I'm using the example key here, I have the correct config
const firebaseConfig = {
  apiKey: "api-key",
  authDomain: "project-id.firebaseapp.com",
  databaseURL: "https://project-id.firebaseio.com",
  projectId: "project-id",
  storageBucket: "project-id.appspot.com",
  messagingSenderId: "sender-id",
  appId: "app-id",
  measurementId: "G-measurement-id",
};

if (firebase.apps.length === 0) {
  initializeApp(firebaseConfig);
}

const db = firebase.firestore();
const auth = firebase.auth();
const storage = firebase.storage();

export { db, auth, storage };

I installed the firebase package with:

expo install firebase

Any help would be greatly appreciated. Thank you!

like image 304
Joshua Gao Avatar asked Nov 02 '21 17:11

Joshua Gao


People also ask

How to resolve “cannot resolve module “firebase from ‘Firebase’ in react JS project”?

Problem: Getting an error "Cannot resolve module "firebase" from 'firebase.js': Firebase could not be found within the project" in react js project. Many times re-install the firebase module but the firebase module not working. Solution: First install the firebase package in your project with this command -

How do I remove unused code from the firebase Web SDK?

The Firebase Web SDK is designed to work with module bundlers to remove any unused code (tree-shaking). We strongly recommend using this approach for production apps. Tools such as the Angular CLI , Next.js, Vue CLI, or Create React App automatically handle module bundling for libraries installed through npm and imported into your codebase.

How do I add Firebase resources to my project?

In the Firebase console, click Add project, then select or enter a Project name. If you have an existing Google Cloud Platform (GCP) project, you can select the project from the dropdown menu to add Firebase resources to that project. (Optional) If you are creating a new project, you can edit the Project ID.

What are the available JavaScript libraries for Firebase?

Firebase provides JavaScript libraries for most Firebase products, including Remote Config, FCM, and more. You can add any of the available libraries to your app.


Video Answer


5 Answers

To reduce the size of the app, firebase SDK (v9.0.0) became modular. You can no longer do the import statement like before on v8.

You have two options.

  1. Use the backwards compatible way. (it will be later removed):

This:

import firebase from 'firebase/app';
import 'firebase/auth';
import 'firebase/firestore';

Should be changed to:

// v9 compat packages are API compatible with v8 code
import firebase from 'firebase/compat/app';
import 'firebase/compat/auth';
import 'firebase/compat/firestore';
  1. Refactor your code now.

From this:

import firebase from "firebase/compat/app";
import "firebase/compat/auth";

const auth = firebase.auth();
auth.onAuthStateChanged(user => { 
  // Check for user status
});

To this:

import { getAuth, onAuthStateChanged } from "firebase/auth";

const auth = getAuth(firebaseApp);
onAuthStateChanged(auth, user => {
  // Check for user status
});

You should definitely check the documentation

like image 127
Enrique VC Avatar answered Oct 23 '22 22:10

Enrique VC


I have met the same issue today, in my case, it has been solved by followings.

1.

// v9 compat packages are API compatible with v8 code
import firebase from 'firebase/compat/app';
import 'firebase/compat/auth';
import 'firebase/compat/firestore';

change from ↓

if (firebase.apps.length === 0) {
  initializeApp(firebaseConfig);
}

to ↓

app = firebase.initializeApp(firebaseConfig)

This link was really helpfull

To ba safe, I share my firebase.js (hiding personal info)

import firebase from 'firebase/compat/app';
import 'firebase/compat/auth';
import 'firebase/compat/firestore';

const firebaseConfig = {
  apiKey: "AIzxxxxxxxxxxxxxxxxxxxBbBFNGMI",
  authDomain: "sixxxxxxxxxcc8.firebaseapp.com",
  projectId: "sxxxxxxxxxxx8",
  storageBucket: "xxxxxxxxxxxxxcc8.appspot.com",
  messagingSenderId: "6xxxxxxxxxx",
  appId: "1:65xxxxxxxx13:web:d0exxxxxxxxxxxxxxxxx7c"
};

let app;

if (firebase.apps.length === 0) {
  app = firebase.initializeApp(firebaseConfig)
} else {
  app = firebase.app();
}

const db = app.firestore();
const auth = firebase.auth();

export { db, auth };
like image 37
K Lee Avatar answered Oct 23 '22 21:10

K Lee


Uninistall firebase, then install [email protected]

like image 6
Bongani Khoza Avatar answered Oct 23 '22 21:10

Bongani Khoza


After alot of research i found that we have not configured metro to read .cjs extensions.

create a metro.config.js file in root folder and paste this code.

const { getDefaultConfig } = require("metro-config");

module.exports = (async () => {
  const {
    resolver: { assetExts },
  } = await getDefaultConfig();
  return {
    transformer: {
      getTransformOptions: async () => ({
        transform: {
          experimentalImportSupport: false,
          inlineRequires: false,
        },
      }),
    },
    resolver: {
      assetExts: [...assetExts, "cjs"],
    },
  };
})();
like image 2
oscar muya Avatar answered Oct 23 '22 22:10

oscar muya


First check your package.json , what is your firebase version.

"firebase": "9.9.1",

if it is more than 9.0.0 you can change it to

"firebase": "8.2.3",

and run

npm install

it work for me.

like image 1
Vidura Silva Avatar answered Oct 23 '22 23:10

Vidura Silva