简体   繁体   English

为什么当 Node.JS 应用程序与 Firebase 集成时它会崩溃?

[英]Why when a Node.JS app is integrated with Firebase it crashes?

First, I've followed Express Getting started and developed a Hello World application.首先,我按照 Express Getting started并开发了一个 Hello World 应用程序。 Second, I've followed Firebase 's guide for Node.JS apps and configured Cloud Functions .其次,我遵循了Firebase的 Node.JS 应用指南并配置了Cloud Functions

Ending up with the following project structure.最终得到以下项目结构。

> bin/
  > www.js
> src/
  > controllers
  > routes
  > more modules
> test/
  > src
> functions/
  > index.js
  > package.json
> app.js
> package.json

The content of functions/index.js is: functions/index.js的内容是:

const functions = require('firebase-functions');
const app = require('../app');
exports.app = functions.https.onRequest(app);

Now, when trying to deploy it fails with the next error.现在,当尝试部署它时失败并出现下一个错误。

Cannot find module '../app'

Root cause根本原因

Cloud Functions uploads the content of the functions/ directory on deployment and so there was no Node.JS app inside it. Cloud Functions 在部署时上传functions/目录的内容,因此其中没有 Node.JS 应用程序。

Solutions解决方案

There's a couple of ways to solve it .有几种方法可以解决它 One way is by copying the app before deploy, following the steps.一种方法是按照以下步骤在部署之前复制应用程序。

  1. Update the app's path in index.js.在 index.js 中更新应用程序的路径。

     -- const app = require('../app'); ++ const app = require('./app');
  2. All dependencies (do not confuse with devDependencies ) from package.json must be installed at functions/package.json . package.json所有dependencies (不要与devDependencies混淆)必须安装在functions/package.json For example: if @google-cloud/firestore is declared as an app's dependency then it must be installed as follows.例如:如果@google-cloud/firestore被声明为应用程序的依赖项,那么它必须按如下方式安装。

     cd functions && npm i @google-cloud/firestore
  3. Copy the app, before deployment, into the Functions module.在部署之前将应用程序复制到 Functions 模块中。 This can be done via the firebase.json file by updating it with the following block.这可以通过firebase.json文件通过使用以下块更新它来完成。

     "functions": { "predeploy": "mkdir functions/app && cp -r {app.js,src} functions/app", "postdeploy": "rm -r functions/app" }

Now deploying again will work.现在再次部署将起作用。

firebase deploy # success!

What is happening is that the Node.JS app is copy and pasted just before the deploy step is ran.发生的事情是 Node.JS 应用程序在运行部署步骤之前被复制和粘贴。 Then the deployment uploads not only the functions but also the required app into the cloud.然后部署不仅将功能上传,还将所需的应用程序上传到云中。 Then when the functions are executed, the app too.然后,当执行功能时,应用程序也会执行。

Impact影响

As mentioned at step #2, the app's dependencies are installed also at the Functions module.如第 2 步所述,应用程序的依赖项也安装在 Functions 模块中。 Otherwise the deployment will fail due missing dependencies.否则部署将因缺少依赖项而失败。 package.json is used locally whereas functions/package.json is used on the cloud. package.json在本地使用,而functions/package.json在云上使用。 By following this approach, you need to keep track of dependencies and make sure same ones are installed in both modules.通过遵循这种方法,您需要跟踪依赖项并确保在两个模块中都安装了相同的依赖项。

Source来源

3 Ways To Integrate a Node.JS App With Firebase 将 Node.JS 应用程序与 Firebase 集成的 3 种方法

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

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