Runtime.ImportModuleError错误:找不到模块'axios / lib / utils'无服务器

时间:2020-04-29 07:45:35

标签: node.js amazon-web-services aws-lambda serverless

我正在使用无服务器框架。后端为node.js。我有几个微服务,所有其他微服务都可以正常工作,但是现在我创建了现在的微服务,其中没有使用Axios,但仍然在控制台中引发错误。 另一个问题是,它在我的本地系统中运行良好,但是当我将其推入服务器时,它开始产生问题。 project directory image

这是引发错误的示例代码

    const { IamAuthenticator } = require('ibm-watson/auth');
    const NaturalLanguageUnderstandingV1 = require('ibm-watson/natural-language-understanding/v1');

    async function textAnalyse(req, res) {
      const naturalLanguageUnderstanding = new NaturalLanguageUnderstandingV1({
      version: '2019-07-12',
      authenticator: new IamAuthenticator({
        apikey: 'API KEY'
      }),
    url: 'https://URL/natural-language-understanding/api'
    });

    const analyzeParams = {
       'text': HtmlToText.fromString('Test text here'),
       'features': {
         'entities': {
           'sentiment': true,
           'limit': 100
          }
        }
    };

    const analysis = await naturalLanguageUnderstanding.analyze(analyzeParams);

    // prepare the response object
    res.send({ analysis: analysis });
  }

AWS Cloud监视中的错误 { "errorType": "Runtime.ImportModuleError", "errorMessage": "Error: Cannot find module 'axios/lib/utils'", "stack": [ "Runtime.ImportModuleError: Error: Cannot find module 'axios/lib/utils'", " 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)", " at bootstrapNodeJSCore (internal/bootstrap/node.js:623:3)" ] }

1 个答案:

答案 0 :(得分:0)

我找到了解决方法。 当我们从 Lambda 调用第三方 API 时,它需要在内部实现 Axios。因此,您需要创建一个文件夹,该文件夹将包含一个具有依赖项的 package.json 文件

"dependencies": {
    "axios": "^0.19.2"
  }

然后在 AWS UI 的函数中添加层,左侧菜单 Steps to Add layer

然后将图层添加到您的函数中 Added layer to the function

现在,通过执行上述活动,问题将得到解决,并且 Axios 依赖项已成功单独添加到微服务中。