简体   繁体   English

AWS Cloud9 Lambda安装node.js模块

[英]AWS Cloud9 Lambda installing node.js modules

I'm having issues trying to install node modules in my lambda project in AWS Cloud9. 我在尝试在AWS Cloud9的lambda项目中安装节点模块时遇到问题。

I ran the commands 我运行了命令

npm install --save request
npm install --save request-promise

I keep getting "Error: Cannot find module 'request-promise'". 我不断收到“错误:找不到模块'request-promise'”。

I have the following code. 我有以下代码。

var rp = require('request-promise');

It is structured like this 它的结构是这样的

-Lambda Env -Lambda Env

--getMergedProducts --getMergedProducts

--node_modules --node_modules

This is not how Lambda should be used. 这不是应使用Lambda的方式。 I spoke with an AWS solutions architect and he set things straight for me. 我与AWS解决方案架构师进行了交谈,他为我解决了所有问题。

Remember that each function is intended to stand on its own, so it needs to be packaged on its own. 请记住,每个功能都旨在独立存在,因此需要独立包装。 Each function would have its own package.json and node_modules as each function runs in its own container(s). 每个函数在其各自的容器中运行时,将具有自己的package.json和node_modules。

Your approach assumes that all of your functions run on the same infrastructure, which is not the case in the serverless world. 您的方法假定所有功能都在同一基础结构上运行,而在无服务器环境中则并非如此。 Global modules (beyond what is packaged in the Lambda runtime) do not exist. 全局模块(超出Lambda运行时中包装的模块)不存在。

Be sure to read: https://docs.aws.amazon.com/lambda/latest/dg/best-practices.html and https://docs.aws.amazon.com/lambda/latest/dg/nodejs-create-deployment-pkg.html 请务必阅读: https : //docs.aws.amazon.com/lambda/latest/dg/best-practices.htmlhttps://docs.aws.amazon.com/lambda/latest/dg/nodejs-create -deployment-pkg.html

And I tested this by moving the node_modules folder from the environment folder to the lambda project folder and it worked! 我通过将node_modules文件夹从环境文件夹移动到lambda项目文件夹进行了测试,并且可以正常工作!

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

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