我使用的是无服务器框架。后端为node.js。我有几个微服务,所有其他的都工作得很好,但现在我已经创建了现在的微服务,其中我没有使用Axios,但仍然在控制台中抛出错误。另一个问题是,在我的本地系统中,它工作得很好,但当我将它推入服务器时,它就会开始产生问题。

这是抛出错误的示例代码
    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 watch {     "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)"     ] }中的错误
发布于 2021-03-31 17:37:18
我找到了解决这个问题的方法。当我们从Lambda调用第三方API时,需要在内部实现Axios。因此,您需要创建一个文件夹,其中包含一个带有依赖项的package.json文件
"dependencies": {
    "axios": "^0.19.2"
  }然后在AWS UI左侧菜单的函数中添加图层

然后将图层添加到函数中

现在,通过执行上述活动,问题将得到解决,Axios依赖项将成功地单独添加到微服务中。
https://stackoverflow.com/questions/61496683
复制相似问题