NAR*_*EEN 5 amazon-web-services node.js aws-lambda serverless
我正在使用无服务器框架。后端为 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 });
}
Run Code Online (Sandbox Code Playgroud)
AWS 云监视中的错误
{
"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)"
]
}
我找到了解决这个问题的方法。当我们从 Lambda 调用第三方 API 时,需要在内部实现 Axios。因此,您需要创建一个文件夹,其中包含带有依赖项的 package.json 文件
"dependencies": {
"axios": "^0.19.2"
}
Run Code Online (Sandbox Code Playgroud)
现在,通过执行上述活动,问题将得到解决,并且 Axios 依赖项已成功单独添加到微服务中。