AWS Lambda运行报错index.handler未定义或未导出,该如何解决?
AWS Lambda 执行报错
index.handler is undefined or not exported解决方案 错误详情
该错误为Lambda Node.js运行时典型的Handler入口找不到错误,完整报错如下:
{ "errorType": "Runtime.HandlerNotFound", "errorMessage": "index.handler is undefined or not exported", "trace": [ "Runtime.HandlerNotFound: index.handler is undefined or not exported", " at Object.module.exports.load (/var/runtime/UserFunction.js:144:11)", " at Object.<anonymous> (/var/runtime/index.js:43:30)", " at Module._compile (internal/modules/cjs/loader.js:999:30)", " at Object.Module._extensions..js (internal/modules/cjs/loader.js:1027:10)", " at Module.load (internal/modules/cjs/loader.js:863:32)", " at Function.Module._load (internal/modules/cjs/loader.js:708:14)", " at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:60:12)", " at internal/main/run_main_module.js:17:47" ] }
对应的函数运行日志:
START RequestId: fa81b4c8-23f6-4f15-899b-dfc680ae9c57 Version: $LATEST 2021-10-29T08:47:21.477Z undefined ERROR Uncaught Exception {"errorType":"Runtime.HandlerNotFound","errorMessage":"index.handler is undefined or not exported","stack":["Runtime.HandlerNotFound: index.handler is undefined or not exported"," at Object.module.exports.load (/var/runtime/UserFunction.js:144:11)"," at Object.<anonymous> (/var/runtime/index.js:43:30)"," at Module._compile (internal/modules/cjs/loader.js:999:30)"," at Object.Module._extensions..js (internal/modules/cjs/loader.js:1027:10)"," at Module.load (internal/modules/cjs/loader.js:863:32)"," at Function.Module._load (internal/modules/cjs/loader.js:708:14)"," at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:60:12)"," at internal/main/run_main_module.js:17:47"]} 2021-10-29T08:47:22.789Z undefined ERROR Uncaught Exception {"errorType":"Runtime.HandlerNotFound","errorMessage":"index.handler is undefined or not exported","stack":["Runtime.HandlerNotFound: index.handler is undefined or not exported"," at Object.module.exports.load (/var/runtime/UserFunction.js:144:11)"," at Object.<anonymous> (/var/runtime/index.js:43:30)"," at Module._compile (internal/modules/cjs/loader.js:999:30)"," at Object.Module._extensions..js (internal/modules/cjs/loader.js:1027:10)"," at Module.load (internal/modules/cjs/loader.js:863:32)"," at Function.Module._load (internal/modules/cjs/loader.js:708:14)"," at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:60:12)"," at internal/main/run_main_module.js:17:47"]} END RequestId: fa81b4c8-23f6-4f15-899b-dfc680ae9c57 REPORT RequestId: fa81b4c8-23f6-4f15-899b-dfc680ae9c57 Duration: 1463.33 ms Billed Duration: 1464 ms Memory Size: 128 MB Max Memory Used: 11 MB Unknown application error occurred Runtime.HandlerNotFound
排查&解决步骤
按以下顺序逐一核对即可解决问题:
- 检查
index.js的导出规则是否符合运行时要求
默认Lambda Node.js运行时使用CommonJS模块规范,必须按以下方式导出handler函数:
注:如果使用ES模块规范,需要在同级目录的// 正确写法1 exports.handler = async (event, context) => { // 业务逻辑代码 return { statusCode: 200, body: '执行完成' } } // 正确写法2 const handler = async (event, context) => { /* 逻辑代码 */ } module.exports.handler = handlerpackage.json中添加"type": "module"配置,同时使用export const handler = async () => {}的写法导出 - 核对Lambda控制台的handler配置
进入Lambda函数配置页的「运行时设置」模块,确认handler参数配置为index.handler。如果你的入口文件名不是index.js或者导出的函数名不是handler,需要对应修改配置,例如入口文件为app.js、导出函数名为entry,则配置应为app.entry。 - 检查部署包的文件结构
如果是手动打包zip上传的部署包,解压后确认index.js位于压缩包根目录,而非嵌套在子文件夹中。常见错误是打包时将整个代码文件夹打包,导致解压后index.js位于src/等子目录下,Lambda在根目录找不到对应文件就会触发该错误。 - 确认运行时版本匹配
如果你使用了ES模块语法,需要确认Lambda运行时版本为Node.js 14.x及以上,低版本运行时不支持ES模块规范。
内容的提问来源于stack exchange,提问作者user630702
相关产品推荐
相关产品推荐

