Reputation: 263
I'm getting the following error whenever I deploy my firebase functions
Error: Error occurred while parsing your function triggers.
Error [ERR_PACKAGE_PATH_NOT_EXPORTED]: No "exports" main defined in D:\Proyectos\socialapp-functions\functions\node_modules\firebase\package.json
at throwExportsNotFound (internal/modules/esm/resolve.js:285:9)
at packageExportsResolve (internal/modules/esm/resolve.js:508:3)
at resolveExports (internal/modules/cjs/loader.js:450:36)
at Function.Module._findPath (internal/modules/cjs/loader.js:490:31)
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:888:27)
at Function.Module._load (internal/modules/cjs/loader.js:746:27)
at Module.require (internal/modules/cjs/loader.js:974:19)
at require (internal/modules/cjs/helpers.js:92:18)
at Object.<anonymous> (D:\Proyectos\socialapp-functions\functions\index.js:4:18)
at Module._compile (internal/modules/cjs/loader.js:1085:14)
My index.js
const app = require("express")();
const firebase = require('firebase');
const functions = require("firebase-functions");
const admin = require("firebase-admin");
const firebaseConfig = {...};
firebase.initializeApp(firebaseConfig);
app.post('/signup', (req, res) => {
const newUser = {
email: req.body.email,
password: req.body.password,
confirmPassword: req.body.confirmPassword,
handle: req.body.handle
};
firebase
.auth()
.createUserWithEmailAndPassword(newUser.email, newUser.password)
.then(data => {
return res.status(201).json({
message: `User ${newUser.handle} created successfully`
});
})
.catch(err => {
console.error(err);
return res.status(500).json({
error: err.code
});
})
})
exports.api = functions.https.onRequest(app);
Edit: I'm also adding my package.json
{
"name": "functions",
"description": "Cloud Functions for Firebase",
"scripts": {
"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": {
"express": "^4.17.3",
"firebase": "^9.6.9",
"firebase-admin": "^10.0.2",
"firebase-functions": "^3.19.0"
},
"devDependencies": {
"firebase-functions-test": "^0.2.0"
},
"private": true
}
I'm pretty much new on using Firebase with Express.js, I'll gladly appreciate any help. Thanks in advance.
Edit. Added my definition for firebase functions container using express.js exports.api = functions.https.onRequest(app);
Upvotes: 2
Views: 2974
Reputation: 50930
It seems you have the new Firebase Modular SDK (v9.0.0+
) installed and then const firebase = require("firebase")
import seems to be the issue. If you want to use web SDK, then try refactoring your code as shown below:
const app = require("express")();
const functions = require("firebase-functions");
const { initializeApp } = require("firebase/app");
const { createUserWithEmailAndPassword, getAuth } = require("firebase/auth");
app.post("/signup", (req, res) => {
const newUser = {...};
return createUserWithEmailAndPassword(
fireAuth,
newUser.email,
newUser.password
)
.then((data) => {
return res.status(201).json({
message: `User ${newUser.handle} created successfully`,
});
})
})
However you don't need Firebase client SDK to create users in a cloud function. You can use the Admin SDK for that as shown below:
const app = require("express")();
const functions = require("firebase-functions");
const admin = require("firebase-admin");
admin.initializeApp();
app.post("/signup", (req, res) => {
const newUser = {...};
return admin
.auth()
.createUser(newUser)
.then((data) => {
return res.status(201).json({
message: `User ${newUser.handle} created successfully`,
});
})
});
exports.api = functions.https.onRequest(app);
Upvotes: 3