无法在 nodejs 中导入模块 'handler' aws lambda 函数
Unable to import module 'handler' aws lambda function in nodejs
我一直收到此错误,但我不知道是什么原因造成的。
我有一个 API,它基于一个条件 post 到另一个 API,
但是我在包装 API.
时遇到了这个错误
这是代码...
handler.js
'use strict';
const axios = require('axios');
module.exports.thumbnailWrapperAPI = (event, context, callback) => {
const incomingData = JSON.parse(event.body);
if(incomingData.source.includes('png') || incomingData.source.includes('jpg')){
const newLocal = 'some endpoint...';
// call image resizing API...
axios.post(newLocal,{
source: incomingData.source,
target: incomingData.target,
width: incomingData.width
})
.then(response => callback(null,response))
.catch(error => callback(error))
} else if(incomingData.source.includes('html')) {
// handle HTML
} else {
//...
};
};
serverless.yaml
service: thumbnailWrapperAPI
provider:
name: aws
runtime: nodejs8.10
region: eu-west-1
functions:
thumbnailWrapperAPI:
handler: handler.thumbnailWrapperAPI
events:
- http:
path: generatethumbnail/
method: post
cors: true
如有任何建议,我们将不胜感激。
错误信息:
Unable to import module 'handler': Error
at Function.Module._resolveFilename (module.js:547:15)
at Function.Module._load (module.js:474:25)
at Module.require (module.js:596:17)
at require (internal/module.js:11:18)
at Object.<anonymous> (/var/task/handler.js:2:15)
at Module._compile (module.js:652:30)
at Object.Module._extensions..js (module.js:663:10)
at Module.load (module.js:565:32)
at tryModuleLoad (module.js:505:12)
at Function.Module._load (module.js:497:3)
好的,我通过删除我的 package.json 然后再次添加它并安装 NOT 作为我的包的开发依赖项来解决它并且它有效。
错误消息并没有太大帮助,但我发现此消息通常是由丢失的 npm 包引起的。如果你在 AWS 控制台测试 lambda,你可以看到具体的细节。
当您使用错误的路径请求模块或文件时,您也会遇到此错误。换句话说,需要一个不存在的 module/file。
它可以是自定义模块或 npm。
请仔细检查所有模块导入路径,确保它们准确无误。
在我的例子中,我的 lambda 表达式同时使用了 python 和 nodejs,但没有将 nodejs lambda 函数的运行时环境设置为 nodejs。以前,我的 serverless.yml 看起来类似于:
provider:
name: aws
runtime: python3.7
stage: dev
region: eu-west-1
profile: my-profile
functions:
nodejs-func:
handler: nodejs_func.handler
events:
- websocket:
route: nodejs-func
python-func:
handler: python_func.handler
events:
- websocket:
route: python-func
干脆给nodejs lambda的运行环境就解决了:
provider:
name: aws
runtime: python3.7
stage: dev
region: eu-west-1
profile: my-profile
functions:
nodejs-func:
handler: nodejs_func.handler
runtime: nodejs10.x # Provide the runtime environment for lambda func
events:
- websocket:
route: nodejs-func
python-func:
handler: python_func.handler
events:
- websocket:
route: python-func
我一直收到此错误,但我不知道是什么原因造成的。
我有一个 API,它基于一个条件 post 到另一个 API, 但是我在包装 API.
时遇到了这个错误这是代码...
handler.js
'use strict';
const axios = require('axios');
module.exports.thumbnailWrapperAPI = (event, context, callback) => {
const incomingData = JSON.parse(event.body);
if(incomingData.source.includes('png') || incomingData.source.includes('jpg')){
const newLocal = 'some endpoint...';
// call image resizing API...
axios.post(newLocal,{
source: incomingData.source,
target: incomingData.target,
width: incomingData.width
})
.then(response => callback(null,response))
.catch(error => callback(error))
} else if(incomingData.source.includes('html')) {
// handle HTML
} else {
//...
};
};
serverless.yaml
service: thumbnailWrapperAPI
provider:
name: aws
runtime: nodejs8.10
region: eu-west-1
functions:
thumbnailWrapperAPI:
handler: handler.thumbnailWrapperAPI
events:
- http:
path: generatethumbnail/
method: post
cors: true
如有任何建议,我们将不胜感激。
错误信息:
Unable to import module 'handler': Error
at Function.Module._resolveFilename (module.js:547:15)
at Function.Module._load (module.js:474:25)
at Module.require (module.js:596:17)
at require (internal/module.js:11:18)
at Object.<anonymous> (/var/task/handler.js:2:15)
at Module._compile (module.js:652:30)
at Object.Module._extensions..js (module.js:663:10)
at Module.load (module.js:565:32)
at tryModuleLoad (module.js:505:12)
at Function.Module._load (module.js:497:3)
好的,我通过删除我的 package.json 然后再次添加它并安装 NOT 作为我的包的开发依赖项来解决它并且它有效。
错误消息并没有太大帮助,但我发现此消息通常是由丢失的 npm 包引起的。如果你在 AWS 控制台测试 lambda,你可以看到具体的细节。
当您使用错误的路径请求模块或文件时,您也会遇到此错误。换句话说,需要一个不存在的 module/file。
它可以是自定义模块或 npm。
请仔细检查所有模块导入路径,确保它们准确无误。
在我的例子中,我的 lambda 表达式同时使用了 python 和 nodejs,但没有将 nodejs lambda 函数的运行时环境设置为 nodejs。以前,我的 serverless.yml 看起来类似于:
provider:
name: aws
runtime: python3.7
stage: dev
region: eu-west-1
profile: my-profile
functions:
nodejs-func:
handler: nodejs_func.handler
events:
- websocket:
route: nodejs-func
python-func:
handler: python_func.handler
events:
- websocket:
route: python-func
干脆给nodejs lambda的运行环境就解决了:
provider:
name: aws
runtime: python3.7
stage: dev
region: eu-west-1
profile: my-profile
functions:
nodejs-func:
handler: nodejs_func.handler
runtime: nodejs10.x # Provide the runtime environment for lambda func
events:
- websocket:
route: nodejs-func
python-func:
handler: python_func.handler
events:
- websocket:
route: python-func