[英]importing the request library into AWS Lambda on node.js
我用const request = require("request")
调用请求库
我在 package.json 中添加了依赖项,我在下面发布了错误。 然而,通过阅读其他帖子,我意识到我可能需要 npm-install 请求模块并将整个 zip 作为文件夹导入。
由于我是节点新手,我想检查是否有另一种方法可以将“require”库导入当前工作目录,或者我在其他地方犯了错误?
这是我的 package.json:
{
"name": "fact",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"ask-sdk-core": "^2.0.0",
"ask-sdk-model": "^1.0.0",
"i18next": "^15.0.5",
"request": "^2.88.0"
}
}
当我测试时它仍然给我以下错误:
Response:
{
"errorType": "Runtime.ImportModuleError",
"errorMessage": "Error: Cannot find module 'request'",
"trace": [
"Runtime.ImportModuleError: Error: Cannot find module 'request'",
" at _loadUserApp (/var/runtime/UserFunction.js:100:13)",
" at Object.module.exports.load (/var/runtime/UserFunction.js:140:17)",
" at Object.<anonymous> (/var/runtime/index.js:45:30)",
" at Module._compile (internal/modules/cjs/loader.js:778:30)",
" at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)",
" at Module.load (internal/modules/cjs/loader.js:653:32)",
" at tryModuleLoad (internal/modules/cjs/loader.js:593:12)",
" at Function.Module._load (internal/modules/cjs/loader.js:585:3)",
" at Function.Module.runMain (internal/modules/cjs/loader.js:831:12)",
" at startup (internal/bootstrap/node.js:283:19)"
]
}
Request ID:
"01b4df3d-e269-4d8a-90a3-7f02b9be9b58"
Function Logs:
START RequestId: 01b4df3d-e269-4d8a-90a3-7f02b9be9b58 Version: $LATEST
2020-04-03T18:02:05.453Z undefined ERROR Uncaught Exception {"errorType":"Runtime.ImportModuleError","errorMessage":"Error: Cannot find module 'request'","stack":["Runtime.ImportModuleError: Error: Cannot find module 'request'"," at _loadUserApp (/var/runtime/UserFunction.js:100:13)"," at Object.module.exports.load (/var/runtime/UserFunction.js:140:17)"," at Object.<anonymous> (/var/runtime/index.js:45:30)"," at Module._compile (internal/modules/cjs/loader.js:778:30)"," at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)"," at Module.load (internal/modules/cjs/loader.js:653:32)"," at tryModuleLoad (internal/modules/cjs/loader.js:593:12)"," at Function.Module._load (internal/modules/cjs/loader.js:585:3)"," at Function.Module.runMain (internal/modules/cjs/loader.js:831:12)"," at startup (internal/bootstrap/node.js:283:19)"]}
END RequestId: 01b4df3d-e269-4d8a-90a3-7f02b9be9b58
REPORT RequestId: 01b4df3d-e269-4d8a-90a3-7f02b9be9b58 Duration: 2159.04 ms Billed Duration: 2200 ms Memory Size: 128 MB Max Memory Used: 19 MB
Unknown application error occurred
Runtime.ImportModuleError
这是我使用请求的位置:对于上下文,它是 Alexa 请求的事件处理程序。
const checkprof_Handler = {
canHandle(handlerInput) {
const request = handlerInput.requestEnvelope.request;
return request.type === 'IntentRequest' && request.intent.name === 'checkprof' ;
},
handle(handlerInput) {
const request = handlerInput.requestEnvelope.request;
const responseBuilder = handlerInput.responseBuilder;
let sessionAttributes = handlerInput.attributesManager.getSessionAttributes();
let say = 'Let me check. ';
let slotStatus = '';
let resolvedSlot;
let slotValues = getSlotValues(request.intent.slots);
// getSlotValues returns .heardAs, .resolved, and .isValidated for each slot, according to request slot status codes ER_SUCCESS_MATCH, ER_SUCCESS_NO_MATCH, or traditional simple request slot without resolutions
// console.log('***** slotValues: ' + JSON.stringify(slotValues, null, 2));
// SLOT: name
if (slotValues.name.heardAs) {
slotStatus += ' slot name was heard as ' + slotValues.name.heardAs + '. ';
var name = slotValues.name.heardAs;
const url = `http://bluetooth-env-test.eba-brqgvwur.us-east-2.elasticbeanstalk.com/WebApp/search.php/
${name}`;
request.get(url, (error, response, body) => {
let json = JSON.parse(body);
console.log('error:', error); // Print the error if one occurred
console.log('statusCode:', response && response.statusCode); // Print the response status code if a response was received
console.log('body:', body); // Print the body
// const theFact = body;
// const speechOutput = theFact;
// this.response.cardRenderer(SKILL_NAME, theFact);
// this.response.speak(speechOutput + " Would you like another fact?").listen("Would you like another fact?");
// this.emit(':responseReady');
});
slotStatus += slotValues.name.heardAs ;
} else {
slotStatus += 'slot name is empty. ';
}
if (slotValues.name.ERstatus === 'ER_SUCCESS_MATCH') {
slotStatus += 'a valid ';
if(slotValues.name.resolved !== slotValues.name.heardAs) {
slotStatus += 'synonym for ' + slotValues.name.resolved + '. ';
} else {
slotStatus += 'match. '
} // else {
//
}
if (slotValues.name.ERstatus === 'ER_SUCCESS_NO_MATCH') {
slotStatus += 'which did not match any slot value. ';
console.log('***** consider adding "' + slotValues.name.heardAs + '" to the custom slot type used by slot name! ');
}
if( (slotValues.name.ERstatus === 'ER_SUCCESS_NO_MATCH') || (!slotValues.name.heardAs) ) {
slotStatus += 'A few valid values are, ' + sayArray(getExampleSlotValues('checkprof','name'), 'or');
}
say += slotStatus;
return responseBuilder
.speak(say)
.reprompt('try again, ' + say)
.getResponse();
},
};
There is no need to create node Module folder in lambda function.Just create a folder and in that install required ie npm install require and add it to layer in lambda function. 欲了解更多信息,请参阅https://www.freecodecamp.org/news/lambda-layers-2f80b9211318/
注意:使用适当的运行时,即所有可用版本的 nodejs
是的,您需要使用 npm 安装库。 据我所知,您有两种简单的方法可以做到这一点,使用aws cloud9或在本地环境中执行并将 lambda 上传到 aws。
为了从您的本地环境中执行此操作,只需将 function 下载为 zip,单击导出 function 并选择“下载部署包”:
这会将 zip 下载到您的本地环境,解压缩并在文件夹中,打开终端并安装您的依赖项:
npm i request
zip 再次文件夹并使用 aws-cli 将其部署到 aws:
aws lambda update-function-code --function-name=your_function_name --zip-file fileb://your_zip_file_name.zip
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.