简体   繁体   English

部署 Firebase 函数时出错:无法找到模块

[英]Error while deploying Firebase functions: Unable to find module

My code: Inside of an index.js file.我的代码:在 index.js 文件中。 Set up using firebase init command, functions selected.使用 firebase init 命令进行设置,选择功能。 I'm able to deploy without error if I comment out the code:如果我注释掉代码,我就可以无误地进行部署:

const firebase = require("firebase");
firebase.initializeApp(config);

It throws the error if I leave this in. I've tried running npm install and npm rebuild in both the functions folder and the parent folder.如果我把它留在里面,它会抛出错误。我试过在函数文件夹和父文件夹中运行 npm install 和 npm rebuild 。

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

const config = {
  apiKey: "apiKey",
  authDomain: "socialapp.firebaseapp.com",
  databaseURL: "https://socialapp.firebaseio.com"
  storageBucket: "socialapp.appspot.com"
};

const firebase = require("firebase");
firebase.initializeApp(config);

app.get("/screams", (req, res) => {
  admin
    .firestore()
    .collection("screams")
    .orderBy("createdAt", "descending")
    .get()
    .then(data => {
      let screams = [];
      data.forEach(doc => {
        screams.push({
          screamId: doc.id,
          body: doc.data().body,
          userHandle: doc.data().userHandle,
          createdAt: doc.data().createdAt
        });
      });
      return res.json(screams);
    })
    .catch(err => console.log(err));
});

app.post("/scream", (req, res) => {
  const newScream = {
    body: req.body.body,
    userHandle: req.body.userHandle,
    createdAt: new Date().toISOString()
  };
  admin
    .firestore()
    .collection("screams")
    .add(newScream)
    .then(doc => {
      res.json({ message: `document ${doc.id} created successfully` });
    })
    .catch(err => {
      res.status(500).json({ error: "Something went wrong." });
      console.error(err);
    });
});

//Signup Route

app.post("/signup", (req, res) => {
  const newUser = {
    email: req.body.email,
    password: req.body.password,
    confirmPassword: req.body.confirmPassword,
    handle: req.body.handle
  };
  //Todo validate data

  firebase
    .auth()
    .createUserWithEmailAndPassword(newUser.email, newUser.password)
    .then(data => {
      return res
        .status(201)
        .json({ message: `user ${data.user.uid} signed up successfully` });
    })
    .catch(err => {
      console.err(error);
      return res.status(500).json({ error: err.code });
    });
});

exports.api = functions.https.onRequest(app);

Error from console:来自控制台的错误:

i  deploying functions
i  functions: ensuring necessary APIs are enabled...
+  functions: all necessary APIs are enabled
i  functions: preparing functions directory for uploading...

Error: Error parsing triggers: The gRPC binary module was not installed. This may be fixed by running "npm rebuild"
Original error: Cannot find module 'C:\Users\alex_\Desktop\socialapp-functions\functions\node_modules\grpc\src\node\extension_binary\node-v79-win32-x64-unknown\grpc_node.node'
Require stack:
- C:\Users\alex_\Desktop\socialapp-functions\functions\node_modules\grpc\src\grpc_extension.js
- C:\Users\alex_\Desktop\socialapp-functions\functions\node_modules\grpc\src\client_interceptors.js
- C:\Users\alex_\Desktop\socialapp-functions\functions\node_modules\grpc\src\client.js
- C:\Users\alex_\Desktop\socialapp-functions\functions\node_modules\grpc\index.js
- C:\Users\alex_\Desktop\socialapp-functions\functions\node_modules\@firebase\firestore\dist\index.node.cjs.js
- C:\Users\alex_\Desktop\socialapp-functions\functions\node_modules\firebase\dist\index.node.cjs.js
- C:\Users\alex_\Desktop\socialapp-functions\functions\index.js
- C:\Users\alex_\AppData\Roaming\npm\node_modules\firebase-tools\lib\triggerParser.js

Try running "npm install" in your functions directory before deploying.

Having trouble? Try firebase [command] --help

You don't need the "firebase" module here (it's mostly meant for web application, note nodejs apps).您不需要此处的“firebase”模块(它主要用于 Web 应用程序,注意 nodejs 应用程序)。 You can use the Admin SDK instead to create a user account as described in the documentation .您可以改为使用 Admin SDK 创建用户帐户,如文档中所述。

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

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