You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

AWS Amplify部署Lambda遇index.handler未定义问题求助

问题描述

使用AWS Amplify部署Lambda函数后,触发时抛出Runtime.HandlerNotFound错误,日志显示index.handler is undefined or not exported,但本地确认handler已存在且导出。相关代码与配置如下:

index.ts代码

import { graphqlHandler } from '@/graphql';

console.log(graphqlHandler);

exports.handler = graphqlHandler;

Webpack配置

const CopyPlugin = require("copy-webpack-plugin");
const EventHooksPlugin = require('event-hooks-webpack-plugin');
const fs = require('fs-extra');
const path = require('path');

module.exports = {
    mode: 'production',
    entry: './build/index.ts',
    target: 'node',
    module: {
        rules: [
            {
                test: /\.tsx?$/,
                use: 'ts-loader',
                exclude: /node_modules/,
            },
        ],
    },
    resolve: {
        alias: {
            "@/graphql": path.resolve(__dirname, './build/graphql'),
            "@/generated": path.resolve(__dirname, './build/generated'),
        },
        extensions: ['.tsx', '.ts', '.js'],
    },
    output: {
        path: path.resolve(__dirname, './src'),
        filename: 'index.js',
    },
    plugins: [
        new EventHooksPlugin({
            'beforeRun': new EventHooksPlugin.CallbackTask((compilation, callback) => {
                return Promise.all([
                    new Promise((resolve) => fs.copy('../../../../graphql', './build/graphql', resolve)),
                    new Promise((resolve) => fs.copy('../../../../generated', './build/generated', resolve)),
                    new Promise((resolve) => fs.copy('./lib/index.ts', './build/index.ts', resolve)),
                    new Promise((resolve) => fs.copy('./lib/package.json', './build/package.json', resolve)),
                ]).then(() => callback());
            }),
        }),
        new CopyPlugin({
            patterns: [
                { from: "./build/package.json", to: "./package.json" },
                { from: "../../../../schema.graphql", to: "./schema.graphql" },
                { from: "../../../../prisma/schema.prisma", to: "./schema.prisma"},
                {
                    from: "../../../../node_modules/.prisma/client/libquery_engine-rhel-openssl-1.0.x.so.node",
                    to: "./libquery_engine-rhel-openssl-1.0.x.so.node"
                },
            ],
        }),
    ],
};

tsconfig.json配置

{
  "compilerOptions": {
    "allowSyntheticDefaultImports": true,
    "paths": {
      "@/*": ["./build/*"]
    }
  },
}

执行日志

2023-07-20T12:59:34.409Z    undefined   INFO    [AsyncFunction (anonymous)]
2023-07-20T12:59:34.421Z    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 UserFunction.js.module.exports.load (file:///var/runtime/index.mjs:1032:15)",
        "    at async start (file:///var/runtime/index.mjs:1192:23)",
        "    at async file:///var/runtime/index.mjs:1198:1"
    ]
}
解决方案

1. 调整Webpack输出配置,确保handler正确暴露

Webpack生产模式下的默认优化可能导致exports.handler未被正确挂载到模块导出对象。修改Webpack的output字段,添加libraryTarget: 'commonjs2':

output: {
    path: path.resolve(__dirname, './src'),
    filename: 'index.js',
    libraryTarget: 'commonjs2' // 新增配置
},

该配置强制Webpack以CommonJS2规范导出模块,让Lambda运行时能正确识别module.exports上的handler。

2. 统一导出语法并适配TypeScript配置

将CommonJS导出改为ES模块语法,同时调整TypeScript配置确保编译后兼容Lambda环境:

  • 修改index.ts:
import { graphqlHandler } from '@/graphql';

console.log(graphqlHandler);

export const handler = graphqlHandler;
  • 更新tsconfig.json:
{
  "compilerOptions": {
    "allowSyntheticDefaultImports": true,
    "paths": {
      "@/*": ["./build/*"]
    },
    "module": "commonjs",
    "esModuleInterop": true
  },
}

3. 验证打包后的输出文件

打包完成后,检查生成的src/index.js,确认末尾存在类似以下的导出语句:

module.exports.handler = graphqlHandler;

若不存在,检查Webpack入口文件路径是否正确,以及beforeRun钩子中的文件复制逻辑是否将最新的index.ts同步到了build目录。

4. 确认Amplify的Lambda handler配置

在Amplify控制台或amplify/backend/function/[你的函数名]/function.json中,确保handler配置为index.handler,与打包后的文件名和导出函数名完全一致。


内容的提问来源于stack exchange,提问作者cscan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.15 16:33:13