简体   繁体   English

在 Firebase Functions 和 React 之间共享代码

[英]Sharing code between Firebase Functions and React

I'm using Firebase functions with a React application.我在 React 应用程序中使用 Firebase 函数。 I have some non-trivial code that I don't want to duplicate, so I want to share it between the deployed functions and my React client.我有一些我不想复制的重要代码,所以我想在部署的函数和我的 React 客户端之间共享它。 I've got this working locally in my React client (though I haven't tried deploying) - but I can't deploy my functions.我已经在我的 React 客户端中本地工作了(尽管我还没有尝试部署)——但我无法部署我的功能。

The first thing I tried was npm link.我尝试的第一件事是 npm 链接。 This worked locally, but the functions won't deploy (which makes sense, since this leaves no dependency in your package.json).这在本地有效,但函数不会部署(这是有道理的,因为这不会在您的 package.json 中留下任何依赖关系)。 Then I tried npm install../shared/ - this looked promising because it did leave a dependency in package.json with a file: prefix - but Firebase still won't deploy with this (error below).然后我尝试了 npm install../shared/ - 这看起来很有希望,因为它确实在 package.json 中留下了一个依赖文件:前缀 - 但 Firebase 仍然不会部署这个(下面的错误)。

My project directory structure looks like this:我的项目目录结构如下所示:

/ProjectDir
  firebase.json
  package.json (for the react app)
  /src
    * (react source files)
  /functions
    package.json (for firebase functions)
    index.js
  /shared
    package.json (for the shared module)
    index.js

My shared module package.json (extraneous details omitted):我的共享模块 package.json(省略无关的细节):

{
  "name": "myshared",
  "scripts": {
  },
  "dependencies": {
  },
  "devDependencies": {
  },
  "engines": {
    "node": "8"
  },
  "private": true,
  "version": "0.0.1"
}

My firebase functions package.json (extraneous details omitted):我的 firebase 函数 package.json(省略无关的细节):

{
  "name": "functions",
  "scripts": {
  },
  "dependencies": {
    "myshared": "file:../shared",
  },
  "devDependencies": {
  },
  "engines": {
    "node": "8"
  },
  "private": true
}

When I try to deploy with:当我尝试部署时:

firebase deploy --only functions

It's telling me it can't load the module:它告诉我它无法加载模块:

Function failed on loading user code. Error message: Code in file index.js can't be loaded.
Did you list all required modules in the package.json dependencies?

And I don't think the issue is how I export/imported my code- but just in case: The export:而且我认为问题不在于我如何导出/导入我的代码 - 但以防万一:导出:

exports.myFunc = () => { some code };

The import (functions/index.js)导入(函数/index.js)

const myFunc = require('myshared');

And in my react code:在我的反应代码中:

import { myFunc } from 'myshared';

So far the searching I've done hasn't yielded anything that works.到目前为止,我所做的搜索没有产生任何有效的结果。 Someone did mention entering the shared module path in firebase.json, but I couldn't find any details (including in the firebase docs) that show what that would look like.有人确实提到在 firebase.json 中输入共享模块路径,但我找不到任何详细信息(包括在 firebase 文档中)显示它的外观。 Thanks for any tips to get this going.感谢您提供任何提示来实现这一目标。

I found a solution.我找到了解决办法。 I'm not sure if it's the only or even the best solution, but it seems to work for this scenario, and is easy.我不确定它是唯一还是最好的解决方案,但它似乎适用于这种情况,而且很简单。 As Doug noted above, Firebase doesn't want to upload anything not in the functions directory.正如 Doug 上面提到的,Firebase 不想上传任何不在 functions 目录中的东西。 The solution was to simply make my shared module a subdirectory under functions (ie./functions/shared/index.js).解决方案是简单地使我的共享模块成为函数下的子目录(即./functions/shared/index.js)。 I can then import into my functions like a normal js file.然后我可以像普通的 js 文件一样导入我的函数。 However, my shared folder also has a package.json, for use as a dependency to the react app.但是,我的共享文件夹也有一个 package.json,用作 React 应用程序的依赖项。 I install it using:我安装它使用:

npm install ./functions/shared

This creates a dependency in my react app, which seems to resolve correctly.这在我的反应应用程序中创建了一个依赖项,它似乎可以正确解析。 I've created a production build without errors.我创建了一个没有错误的生产版本。 I haven't deployed the react app yet, but I don't think this would be an issue.我还没有部署 React 应用程序,但我认为这不是问题。

Another solution is to create a symlink.另一种解决方案是创建符号链接。 In terminal, under /ProjectDir, execute:在终端的 /ProjectDir 下,执行:

ln -s shared functions/shared
cd functions
npm i ./shared

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

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