部署到云功能 - "Error: cannot find module"



我在尝试部署函数时得到了这个:

Deployment error.
Build failed: npm ERR! code ENOLOCAL
npm ERR! Could not install from "../config/myconfig" as it does not contain a package.json file.

这是在按照官方文档的建议更新我的functions目录中的package.json之后:

...
"dependencies": {
"sms_sender": "file:./",
"myconfig": "file:../config/myconfig",
"firebase-admin": "^8.10.0",
"firebase-functions": "^3.6.1"
},
...

它应该修复这个错误,如在GCP日志上的堆栈中所见:

A 2020-08-27T12:43:39.026Z sendMessage Could not load the function, shutting down. sendMessage
A 2020-08-27T12:43:39.020Z sendMessage     at Function.Module._load (internal/modules/cjs/loader.js:585:3) sendMessage
A 2020-08-27T12:43:39.020Z sendMessage     at tryModuleLoad (internal/modules/cjs/loader.js:593:12) sendMessage
A 2020-08-27T12:43:39.020Z sendMessage     at Module.load (internal/modules/cjs/loader.js:653:32) sendMessage
A 2020-08-27T12:43:39.020Z sendMessage     at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10) sendMessage
A 2020-08-27T12:43:39.019Z sendMessage     at Module._compile (internal/modules/cjs/loader.js:778:30) sendMessage
A 2020-08-27T12:43:39.019Z sendMessage     at Object.<anonymous> (/workspace/sms_sender.js:5:16) sendMessage
A 2020-08-27T12:43:39.019Z sendMessage     at require (internal/modules/cjs/helpers.js:25:18) sendMessage
A 2020-08-27T12:43:39.019Z sendMessage     at Module.require (internal/modules/cjs/loader.js:692:17) sendMessage
A 2020-08-27T12:43:39.019Z sendMessage     at Function.Module._load (internal/modules/cjs/loader.js:562:25) sendMessage
A 2020-08-27T12:43:39.019Z sendMessage     at Function.Module._resolveFilename (internal/modules/cjs/loader.js:636:15) sendMessage
A 2020-08-27T12:43:39.019Z sendMessage Detailed stack trace: Error: Cannot find module '../config/myconfig' sendMessage
A 2020-08-27T12:43:39.019Z sendMessage Did you list all required modules in the package.json dependencies? sendMessage
A 2020-08-27T12:43:39.019Z sendMessage Provided module can't be loaded. sendMessage

sendMessage是我在index.js中的后台函数,它导入"sms_sender"模块:

exports.sendMessage = functions.https.onRequest(async (req, res) => {
...
// send the sendMessage
sms_sender.sendMsg("+123567890", "hello, my friend!");
...
}

sms_sender.js:

const config = require('../config/myconfig');
module.exports = {
sendMsg: async function(recipientNum, message) {
...
}
}

项目结构:

/project-root
/config
myconfig.js
/functions
index.js
sms_sender.js
package.json
/tests
...
package.json

我看了这篇相关的帖子,但它谈论的是一种外部依赖。

我应该如何告诉Node/npm"myconfig"只是一个本地文件,而不是一个模块本身?我试着在/config中放入一个存根"package.json"文件,但我仍然得到了相同的Could not install from "../config/myconfig" as it does not contain a package.json file.错误

请确保从函数目录而不是项目目录安装包。它在本地工作,当你部署它时会产生错误。所以,

cd functions
npm install [your package]

它会很好地工作

您需要在functions目录中包含您想要的所有文件,因为只有这些文件被打包并上传以创建云函数的源。如果您将config移到functions目录中,您应该会有更好的运气。

关于Firebase工具回购的相关讨论有些帮助,但最流行的答案(在./functions目录下创建一个tarball(对我来说并不太管用。

工作原理:./functions中创建根级./config文件夹的符号链接

cd functions
ln -s ../config/ config

该策略将config中的所有文件都用于功能模块,而不会导致我重写我的应用程序的所有其他部分,这些部分依赖于所描述的文件/文件夹结构

我唯一要做的其他改变:

// functions/package.json
...
"dependencies": {
"sms_sender": "file:./",
"myconfig": "file:./config/",
"firebase-admin": "^8.10.0",
"firebase-functions": "^3.6.1"
},
...

// sms_sender.js
const config = require('./config/myconfig');
... // everything else is as before

相关内容

  • 没有找到相关文章

最新更新