Firebase 函数部署失败
Firebase functions failed to deploy
每当我部署我的 firebase 函数时,我都会收到以下错误
错误:解析您的函数触发器时出错。
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)
我的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);
编辑:我也在添加我的 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
}
我对将 Firebase 与 Express.js 结合使用还很陌生,非常感谢您的帮助。提前致谢。
编辑。使用 express.js exports.api = functions.https.onRequest(app);
添加了我对 firebase 函数容器的定义
您似乎安装了新的 Firebase Modular SDK (v9.0.0+
),然后 const firebase = require("firebase")
导入似乎是问题所在。如果您想使用 web SDK,请尝试重构您的代码,如下所示:
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`,
});
})
})
但是,您不需要 Firebase 客户端 SDK 即可在云函数中创建用户。您可以使用 Admin SDK,如下所示:
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);
每当我部署我的 firebase 函数时,我都会收到以下错误
错误:解析您的函数触发器时出错。
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)
我的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);
编辑:我也在添加我的 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
}
我对将 Firebase 与 Express.js 结合使用还很陌生,非常感谢您的帮助。提前致谢。
编辑。使用 express.js exports.api = functions.https.onRequest(app);
添加了我对 firebase 函数容器的定义您似乎安装了新的 Firebase Modular SDK (v9.0.0+
),然后 const firebase = require("firebase")
导入似乎是问题所在。如果您想使用 web SDK,请尝试重构您的代码,如下所示:
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`,
});
})
})
但是,您不需要 Firebase 客户端 SDK 即可在云函数中创建用户。您可以使用 Admin SDK,如下所示:
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);