Lambda: index.handler 未定义或未导出。

huangapple go评论135阅读模式
英文:

Lambda: index.handler is undefined or not exported

问题

我已经部署了一个Lambda函数,使用AWS Amplify,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"
    ]
}

index.js文件及其依赖项已被加载,处理程序存在并且已导出。如何让Lambda执行此函数?

英文:

I have deployed a lambda function using aws amplify, index.ts is as follows:

import { graphqlHandler } from '@/graphql';
console.log(graphqlHandler);
exports.handler = graphqlHandler;

Here's the webpack config:

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"
},
],
}),
],
};

And the tsconfig.json

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

When I execute the function, the following is logged:

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"
]
}

The index.js file and its dependencies are being loaded, the handler is present, and the handler is exported. How can I get the lambda to execute this function?

答案1

得分: 1

Webpack搞乱了输出。我按如下方式更新了Webpack配置:

module.exports = {
    ...
    output: {
        ...
        library: {
            type: 'commonjs2',
        },
    },
};
英文:

Webpack was messing around with the exports. I updated the webpack config as follows:

module.exports = {
...
output: {
...
library: {
type: 'commonjs2',
},
},
};

huangapple
  • 本文由 发表于 2023年7月20日 21:07:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/76730243.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定