繁体   English   中英

部署到 Firestore Cloud Functions - “错误:找不到模块”

[英]Deploying to Firestore Cloud Functions - “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' 只是一个本地文件,而不是一个真正的模块本身? 我尝试将存根 'package.json' 文件放入 /config 但我仍然得到相同的Could not install from "../config/myconfig" as it does not contain a package.json file. 错误

确保您从功能目录而不是项目目录安装了包。 它在本地工作并在您部署时产生错误。 所以,

cd functions
npm install [your package]

它会正常工作

您需要在functions目录中包含您想要的所有文件,因为只有这些文件被打包并上传以创建Cloud Function 的源代码。如果您将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

暂无
暂无

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

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