Runtime.ImportModuleError Error: Cannot find module 'axios/lib/utils' Serverless
Runtime.ImportModuleError Error: Cannot find module 'axios/lib/utils' 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 });
}
AWS Cloud 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)"
]
}
我找到了解决这个问题的方法。
当我们从 Lambda 调用第三方 API 时,它需要在内部实现 Axios。因此,您需要创建一个文件夹,该文件夹将包含一个 package.json 文件和依赖项
"dependencies": {
"axios": "^0.19.2"
}
然后在来自 AWS UI 的函数中添加图层,左侧菜单
然后将图层添加到您的函数中
现在,通过上述activity问题将得到解决,Axios 依赖项已成功单独添加到微服务中。
我正在使用无服务器框架。后端为 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 Cloud 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)"
]
}
我找到了解决这个问题的方法。 当我们从 Lambda 调用第三方 API 时,它需要在内部实现 Axios。因此,您需要创建一个文件夹,该文件夹将包含一个 package.json 文件和依赖项
"dependencies": {
"axios": "^0.19.2"
}
然后在来自 AWS UI 的函数中添加图层,左侧菜单
然后将图层添加到您的函数中
现在,通过上述activity问题将得到解决,Axios 依赖项已成功单独添加到微服务中。