Cloud Functions for Firebase 中的本地依赖项

skw*_*wny 4 node.js firebase google-cloud-functions

当我尝试部署我的函数时,firebase 抱怨这个:

const admin = require('firebase-admin');
const functions = require('firebase-functions');
const C = require('../both/constants.js');

exports.myFunc = functions.database.ref('aRef').onCreate((event) => {
  // blah blah
}

Error: Error parsing triggers: Cannot find module '../both/constants.js'
Run Code Online (Sandbox Code Playgroud)

但不是这个:

const admin = require('firebase-admin');
const functions = require('firebase-functions');

// This is the invite that goes to organizations.
exports.myFunc = functions.database.ref('aRef').onCreate((event) => {
  const C = require('../both/constants.js');
}
Run Code Online (Sandbox Code Playgroud)

这是为什么?

更新:看起来会发生这种情况是因为该函数直到运行时才会查找依赖项。这当然是一个问题,因为仍然需要依赖项。我应该提到我的项目是这样的:

both
  constants.js
functions
  index.js
  myFunc.js
Run Code Online (Sandbox Code Playgroud)

更新 2既然我正在将堆栈完全转移到 Firebase 函数,我又重新开始处理这个问题。模块需要发布才能在函数内工作。对于发生在此psot上的任何人,仅供参考。

Dou*_*son 10

functions不支持将函数代码放在目录以外的任何地方。请注意,当您部署时,它会说:

i  functions: preparing functions directory for uploading...
i  functions: packaged functions (34.06 KB) for uploading
?  functions: functions folder uploaded successfully
Run Code Online (Sandbox Code Playgroud)

它只查看functions及其所有子文件夹。

如果我部署的代码需要在 之外的代码functions,我还会在控制台中看到“找不到模块”错误。

  • 我明白。我想访问我在我的应用程序中使用的常量,包括客户端和“服务器”。 (2认同)
  • 您可以将公共模块安装到服务器模块中,使其成为 node_modules 的一部分。您只需要记住每次更改时执行该安装过程。 (2认同)