将请求库导入到node.js上的AWS Lambda中

德鲁夫

我正在调用请求库 const request = require("request")

在此处输入图片说明

并且我在package.json中添加了依赖项,并在下面发布了错误。但是从阅读其他帖子后,我意识到我可能需要npm-install请求模块并将整个zip导入为文件夹。

由于我是node的新手,所以我想检查是否有另一种方法可以将“ 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();
    },
};
海梅(Jaime S)

是的,您需要使用npm安装该库。据我所知,您有两种简单的方法可以执行此操作,即使用aws cloud9或在您的本地环境中执行并将lambda上载到aws。

为了从您的本地环境中执行此操作,只需以zip格式下载功能,然后单击导出功能并选择“下载部署包”:

在此处输入图片说明

这会将zip文件下载到您的本地环境,将其解压缩并放在文件夹中,打开终端并安装依赖项:

npm i request

再次压缩文件夹,然后使用aws-cli将其部署到aws:

aws lambda update-function-code --function-name=your_function_name --zip-file fileb://your_zip_file_name.zip

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

将Node.js项目导入到Eclipse IDE中

来自分类Dev

将密钥对导入到Amazon AWS中-指纹错误?

来自分类Dev

将JS包导入到所需文件中

来自分类Dev

如何从AWS Certificate Manager导出我的域证书以导入到Java密钥库中?

来自分类Dev

将转储/ SQL文件导入到我在Linode上的PostgreSQL数据库中

来自分类Dev

将转储/ SQL文件导入到我的Linode上的PostgreSQL数据库中

来自分类Dev

将路线存储在文件中并导入到App.js中

来自分类Dev

将CSV文件导入到Ubuntu 16.04上安装的R Studio中

来自分类Dev

AWS Lambda中的HTTP请求

来自分类Dev

将tweenMax导入到javascript中

来自分类Dev

从流导入到数据库后,node.js如何同步最终回调

来自分类Dev

使用SDK将ODI 12c智能导入到Exec存储库中的行为就像导入到Dev存储库中一样

来自分类Dev

从Windows中的.csv文件将数据导入到mongodb数据库中

来自分类Dev

将数据库中的日期导入到Excel工作表中

来自分类Dev

将本地的 mongo 数据库表单导入到 Digital Ocean 上的 Ubuntu Droplet?

来自分类Dev

如何将库导入到基于JavaScript的UWP应用服务中?

来自分类Dev

如何将C ++头文件从静态库导入到Obj-C中?

来自分类Dev

如何将超过100000条记录导入到mysql数据库中?

来自分类Dev

System.Data.SqlClient将nuget包导入到库中

来自分类Dev

如何将包含库本身的python脚本导入到colab笔记本中

来自分类Dev

使用R将多个csv文件导入到postgresql数据库中(内存错误)

来自分类Dev

如何将一张excel文件导入到access数据库中?

来自分类Dev

如何将这种excel格式导入到这种数据库格式中?

来自分类Dev

SAPUI5中的Javascript-需要对简单方法调用进行JS初始化,或如何将“基于js的需求”库导入到SAP Business Objects

来自分类Dev

AWS Lambda中的简单node.js示例

来自分类Dev

在node.js中的AWS Lambda函数之间共享代码

来自分类Dev

在AWS Lambda node.js中获取机密

来自分类Dev

在node.js中的AWS Lambda函数之间共享代码

来自分类Dev

使用node-csv和meteor-file将CSV导入到集合中

Related 相关文章

  1. 1

    将Node.js项目导入到Eclipse IDE中

  2. 2

    将密钥对导入到Amazon AWS中-指纹错误?

  3. 3

    将JS包导入到所需文件中

  4. 4

    如何从AWS Certificate Manager导出我的域证书以导入到Java密钥库中?

  5. 5

    将转储/ SQL文件导入到我在Linode上的PostgreSQL数据库中

  6. 6

    将转储/ SQL文件导入到我的Linode上的PostgreSQL数据库中

  7. 7

    将路线存储在文件中并导入到App.js中

  8. 8

    将CSV文件导入到Ubuntu 16.04上安装的R Studio中

  9. 9

    AWS Lambda中的HTTP请求

  10. 10

    将tweenMax导入到javascript中

  11. 11

    从流导入到数据库后,node.js如何同步最终回调

  12. 12

    使用SDK将ODI 12c智能导入到Exec存储库中的行为就像导入到Dev存储库中一样

  13. 13

    从Windows中的.csv文件将数据导入到mongodb数据库中

  14. 14

    将数据库中的日期导入到Excel工作表中

  15. 15

    将本地的 mongo 数据库表单导入到 Digital Ocean 上的 Ubuntu Droplet?

  16. 16

    如何将库导入到基于JavaScript的UWP应用服务中?

  17. 17

    如何将C ++头文件从静态库导入到Obj-C中?

  18. 18

    如何将超过100000条记录导入到mysql数据库中?

  19. 19

    System.Data.SqlClient将nuget包导入到库中

  20. 20

    如何将包含库本身的python脚本导入到colab笔记本中

  21. 21

    使用R将多个csv文件导入到postgresql数据库中(内存错误)

  22. 22

    如何将一张excel文件导入到access数据库中?

  23. 23

    如何将这种excel格式导入到这种数据库格式中?

  24. 24

    SAPUI5中的Javascript-需要对简单方法调用进行JS初始化,或如何将“基于js的需求”库导入到SAP Business Objects

  25. 25

    AWS Lambda中的简单node.js示例

  26. 26

    在node.js中的AWS Lambda函数之间共享代码

  27. 27

    在AWS Lambda node.js中获取机密

  28. 28

    在node.js中的AWS Lambda函数之间共享代码

  29. 29

    使用node-csv和meteor-file将CSV导入到集合中

热门标签

归档