【问题标题】:Deploying to Firestore Cloud Functions - "Error: cannot find module"部署到 Firestore Cloud Functions - “错误:找不到模块”
【发布时间】:2020-12-16 20:48:24
【问题描述】:

我在尝试部署函数时遇到了这个问题:

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 目录as suggested by the official docs 中的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

我查看了这个related post,但它在谈论外部依赖项。

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

【问题讨论】:

    标签: node.js firebase npm google-cloud-firestore google-cloud-functions


    【解决方案1】:

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

    【讨论】:

    • 嗯,我想过,但config 打算在整个项目中使用(用于环境设置代码、存储机密等)。这不违反关注点分离的原则吗?将config 塞入functions 之下需要重组我项目的很大一部分并且感觉“不自然”,github.com/firebase/firebase-tools/issues/… 也表达了类似的情绪
    【解决方案2】:

    Firebase 工具 repo 上的这个 related discussion 有点帮助,但 most popular answer(在 ./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
    

    【讨论】:

      【解决方案3】:

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

      cd functions
      npm install [your package]
      

      它会正常工作

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-09-18
        • 2020-04-30
        • 1970-01-01
        • 2019-12-12
        • 2017-11-05
        • 2021-03-17
        • 1970-01-01
        • 2018-06-17
        相关资源
        最近更新 更多