menu

Questions & Answers

Firebase Functions Deploy Error [ERR_PACKAGE_PATH_NOT_EXPORTED]: No "exports" main defined

Whenever i try to deploy firebase functions i get the following error:

Failed to load function definition from source: Failed to generate manifest from function source: Error [ERR_PACKAGE_PATH_NOT_EXPORTED]: No "exports" main defined in ...\firebase\package.json

All solution in stackoverflow seem to point to an error in the import of packages but i have followed the official firebase docs here and my imports look exactly the same. Not to mention that this code was working a few weeks ago and now it just fails to deploy. i have also updated my firebase-functions, firebase-admin and firebase-tools packages to the latest versions.

Below is my file structure:

enter image description here

Below is my index.js file code:

const functions = require("firebase-functions");
const admin = require("firebase-admin");
admin.initializeApp();

const firebaseConfig = {
//my firebase settings
};
// Initialize Firebase
const firebase = require("firebase");
firebase.initializeApp(firebaseConfig);

exports.addUserToFirestore = functions.auth.user().onCreate((user) =>{
  const usersRef = admin.firestore().collection("users");
  const providerData = user.providerData;
  providerData.forEach((element) => {
    if (element.providerId=="google.com") {
      return usersRef.doc(user.uid).set({
        uID: user.uid,
      }).then(() => {
        return admin.auth().updateUser(user.uid, {
          emailVerified: true,
          displayName: user.email,
        });
      });
    } else if (element.providerId=="phone") {
      return usersRef.doc(user.uid).set({
        uID: user.uid,
      }).then(() => {
        return admin.auth().updateUser(user.uid, {
          displayName: user.phoneNumber,
        });
      });
    }
  });
});

exports.addEmailUser = functions.https.onCall((data, context) => {
  return admin.auth().createUser({
    email: data.email,
    emailVerified: false,
    password: data.password,
    displayName: data.username,
  }).then((userRecord) => {
    const usersRef = admin.firestore().collection("users");
    return usersRef.doc(userRecord.uid).set({
      uID: userRecord.uid,
    });
  });
});


exports.updateUser = functions.https.
    onCall((data, context) => {
      const usersRef = admin.firestore().collection("users");
      return admin.auth().getUser(context.auth.uid).then((userRecord) =>{
        userRecord.providerData.forEach((element) => {
          if (element.providerId=="google.com") {
            return admin.auth().updateUser(context.auth.uid, {
              displayName: data.username,
            }).then((userRecord) => {
              return usersRef.doc(context.auth.uid).update({
                username: data.username,
              });
            });
          } else if (element.providerId=="phone") {
            return admin.auth().updateUser(context.auth.uid, {
              displayName: data.username,
            }).then((userRecord) => {
              return usersRef.doc(context.auth.uid).update({
                username: data.username,
              });
            });
          } else if (element.providerId=="password") {
            return admin.auth().updateUser(context.auth.uid, {
              email: data.email,
              password: data.password,
              displayName: data.username,
            }).then((userRecord) => {
              return usersRef.doc(context.auth.uid).update({
                email: data.email,
                username: data.username,
              });
            });
          }
        });
      });
    });

exports.addNewSite = functions.https.onCall((data, context) => {
  const usersRef = admin.firestore().collection("users");
  return usersRef.doc(context.auth.uid).collection("passwords").add({
    siteName: data.siteName,
    siteUrl: data.siteUrl,
    logins: data.logins,
  }).then((siteRecord) => {
    return usersRef.doc(context.auth.uid).collection("passwords").
        doc(siteRecord.id).update({
          siteId: siteRecord.id,
          loginsCount: admin.firestore.FieldValue.increment(1),
        }).then(() =>{
          return usersRef.doc(context.auth.uid).collection("passwords").
              doc("data").set({
                documentCount: admin.firestore.FieldValue.increment(1),
              }, {merge: true}).then(() =>{
                return siteRecord.id;
              });
        });
  });
});

exports.deleteUserFromFirestore = functions.auth.user().onDelete((user)=>{
  const usersRef = admin.firestore().collection("users");
  return usersRef.doc(user.uid).collection("passwords").get().
      then((querySnapshot) => {
        querySnapshot.forEach((doc) => {
          usersRef.doc(user.uid).collection("passwords").
              doc(doc.id).delete().catch((error) => {
                return error;
              });
        });
        return usersRef.doc(user.uid).collection("settings").get().
            then((querySnapshot) => {
              querySnapshot.forEach((doc) => {
                usersRef.doc(user.uid).collection("settings").
                    doc(doc.id).delete().catch((error) => {
                      return error;
                    });
              });
              return usersRef.doc(user.uid).delete();
            });
      });
});

Below is my package.json in my functions folder:

{
  "name": "functions",
  "description": "Cloud Functions for Firebase",
  "scripts": {
    "lint": "eslint .",
    "serve": "firebase emulators:start --only functions",
    "shell": "firebase functions:shell",
    "start": "npm run shell",
    "deploy": "firebase deploy --only functions",
    "logs": "firebase functions:log"
  },
  "engines": {
    "node": "16"
  },
  "main": "index.js",
  "dependencies": {
    "firebase": "^9.15.0",
    "firebase-admin": "^11.4.1",
    "firebase-functions": "^4.1.1"
  },
  "devDependencies": {
    "eslint": "^8.9.0",
    "eslint-config-google": "^0.14.0",
    "firebase-functions-test": "^0.2.0"
  },
  "private": true
}

Any help would be appreciated.

Comments:
2023-01-20 23:10:05
Please edit the question to show your project file structure, and be clear about the name and location of the source file you're showing here. There should be enough information in your question that someone could copy and reproduce exactly as you show.
2023-01-20 23:10:05
I have added the file structure @DougStevenson
Answers(1) :

You're using two different SDKs. firebase-admin is for servers and firebase is for clients. I'll assume that you want to use the firebase-admin instead. To fix these, remove these lines of code:

const firebaseConfig = {
//my firebase settings
};
// Initialize Firebase
const firebase = require("firebase");
firebase.initializeApp(firebaseConfig);