简体   繁体   English

在 AWS lambda 函数中使用 require

[英]Using require in AWS lambda functions

I am currently researching AWS lambda functions and I can't find anywhere if I can use the require statement in them so that I can use other, non-lambda functions.我目前正在研究 AWS lambda 函数,如果我可以在其中使用 require 语句以便我可以使用其他非 lambda 函数,我找不到任何地方。 I know about zipping the node modules folder but this doesn't help me here as I don't intend to use a node module, thanks for any answers!我知道压缩节点模块文件夹,但这对我没有帮助,因为我不打算使用节点模块,感谢任何答案!

Just to add to Justin's answer, yes you can require other files in lambda with this structure as an example:只是为了添加到贾斯汀的答案,是的,您可以要求 lambda 中的其他文件具有这种结构作为示例:

| main.js << The handler is here
| func1.js
| func2.js

and in the main file:在主文件中:

// main.js
require('./func1.js');
require('./func2.js');

Yes, you can.是的你可以。 You just have to ensure that the node_modules folder is uploaded as part of your package (you won't be able to use the console editor).您只需确保将node_modules文件夹作为包的一部分上传(您将无法使用控制台编辑器)。 You can read more details on the AWS Blog:您可以在 AWS 博客上阅读更多详细信息:

Using Packages and Native nodejs Modules in AWS Lambda 在 AWS Lambda 中使用包和本机 nodejs 模块

In addition to Naguib Ihab's answer.除了 Naguib Ihab 的回答。 If you want to call function declared in the other JS files, you can declare that function in this way:如果要调用其他 JS 文件中声明的函数,可以这样声明该函数:

exports.handler = async () => {
  return "Hello world"
};

and in your invoking function:并在您的调用函数中:

const funA = require(path-to-file)

invoke the function by通过调用函数

funA.hanlder()

instead of代替

funA()

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

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