【发布时间】:2020-02-22 07:31:05
【问题描述】:
我有一个返回一些动态 HTML 的 http 云函数。我想使用 Handlebars 作为模板引擎。模板足够大,将它放在我的函数顶部的 const 变量中是不切实际的。
我尝试过类似的方法:
const template = fs.readFileSync('./template.hbs', 'utf-8');
但是在部署函数时,我总是得到一个文件不存在的错误:
Error: ENOENT: no such file or directory, open './template.hbs'
template.hbs 与我的 index.js 文件位于同一目录中,所以我想问题是 Firebase CLI 没有将此文件与其余文件捆绑在一起。
根据docs of Google Cloud Functions,可以将本地模块与"mymodule": "file:mymodule" 捆绑在一起。所以我尝试在项目的根目录下创建一个templates 文件夹,并将"templates": "file:./templates" 添加到package.json。
我的文件结构是这样的:
/my-function
index.js
/templates
something.hbs
index.js //this is the entry point
然后:
const template = fs.readFileSync('../node_modules/templates/something.hbs', 'utf-8');
但我遇到了同样的未找到错误。
在 Firebase 云函数中包含和要求非 JS 依赖项的正确方法是什么?
【问题讨论】:
标签: node.js firebase google-cloud-functions