部署到Firestore Cloud Functions-“错误:找不到模块" [英] Deploying to Firestore Cloud Functions - "Error: cannot find module"

查看:50
本文介绍了部署到Firestore Cloud Functions-“错误:找不到模块"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在尝试部署功能时遇到了这个问题:

I'm getting this when trying to deploy functions:

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 目录

This is after even updating the package.json in my functions directory as suggested by the official docs to look like this:

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

应该可以解决此错误,如GCP日志上的stacktrace所示:

which is supposed to fix this error as seen in the stacktrace on the GCP logs:

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'模块:

sendMessage is my background function in index.js which imports the 'sms_sender' module:

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

sms_sender.js:

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

我看了这篇相关文章,但这是在谈论外部依赖性.

I looked at this related post but it was talking about an external dependency.

我应该如何告诉Node/npm'myconfig'只是一个本地文件,而不是一个模块本身?我尝试将存根'package.json'文件放入/config,但仍然得到相同的无法从"../config/myconfig"安装.因为它不包含package.json文件.错误

How should I tell Node/npm that 'myconfig' is just a local file and not really a module per se? I tried putting in a stub 'package.json' file to /config but I still get the same Could not install from "../config/myconfig" as it does not contain a package.json file. error

推荐答案

相关讨论在Firebase工具存储库上有所帮助,但最受欢迎答案(在 ./functions 目录下创建一个tarball)对我来说不是很有效.

This related discussion on the Firebase tools repo was somewhat helpful but the most popular answer (creating a tarball under the ./functions directory) did not quite work for me.

有效方法: ./functions 内的根级别 ./config 文件夹中创建符号链接:

What worked: create a symbolic link of the root-level ./config folder inside of ./functions:

cd functions
ln -s ../config/ config

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

This strategy avails all the files in config into functions modules without causing me to re-write all other parts of my app that relay on the described file/folder structure

我唯一需要做的其他更改:

The only other changes I had to make:

// 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

这篇关于部署到Firestore Cloud Functions-“错误:找不到模块"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆