node.js - 处理程序未定义或未导出

标签 node.js typescript aws-lambda babeljs webpack-5

尝试运行 lambda 函数时出现错误“out/get-user-messages.handler is undefined or not exported”。下载 zip 我看到根目录中的“out”文件夹和里面的 get-user-messages.js。我正在导出处理程序

获取用户消息.ts

import "reflect-metadata";
import { APIGatewayEvent, APIGatewayEventRequestContext, APIGatewayProxyEvent } from "aws-lambda";


export const handler = async (event: APIGatewayProxyEvent, context: APIGatewayEventRequestContext) => { 
/** Code **/
}

tsconfig.json

{
  "compilerOptions": {
    "experimentalDecorators": true,
    "module": "es2015",
    "target": "esnext",
    "noImplicitAny": false,
    "outDir": "./out",
    "strict": false,
    "resolveJsonModule": true,
    "strictNullChecks": false,
    "moduleResolution": "node",
    "emitDecoratorMetadata": true,
    "esModuleInterop": true,    
  },

  "include": ["src/**/*"],
  "exclude": ["node_modules", "**/*.spec.ts", "**/*.test.ts"],
  "paths": {
    "@/*": ["src/*"]
  }
}

webpack.config.js

const ForkTsCheckerWebpackPlugin = require('fork-ts-checker-webpack-plugin');
var path = require('path');

/** @type {import('webpack').Configuration} */
module.exports = {
  mode: "production",
  module: {
    mode: "production",
    rules: [
      {
        test: /\.js|.ts|.tsx/,
        exclude: /node_modules/,
        use: 'babel-loader'
        },
    ],
  },
  resolve: {
    extensions: ['*', '.js', '.jsx', '.tsx', '.ts', '.json'],
  },
  module: {
    rules: [
      {
        test: /\.(ts|js)x?$/,
        exclude: /node_modules/,
        use: ["babel-loader"]
      }
    ]
  },
  plugins: [new ForkTsCheckerWebpackPlugin()],
  externals: ['aws-sdk'], //avoid un-needed modules since aws-sdk exists in aws lambdas already
  entry: {
    //list of compiled files
    "get-user-messages": path.join(__dirname, "./src/lambda/get-user-messages.ts"),
    "insert-user-message": path.join(__dirname, "./src/lambda/insert-user-message.ts"),
    "mark-read-user-message": path.join(__dirname, "./src/lambda/mark-read-user-message.ts"),
    "get-application-release-notes": path.join(__dirname, "./src/lambda/get-application-release-notes.ts"),
    "insert-application-release-note": path.join(__dirname, "./src/lambda/insert-application-release-note.ts")
  },
  target: 'node',
  output: {
    path: path.join(__dirname, "./out"),
    filename: "[name].js",
    library: "[name]",
    libraryTarget: 'commonjs'
  }
};

.babelrc

{
    "presets": [
      ["@babel/preset-env",{"targets":{"node":"14"}, "modules":false}],
      "@babel/preset-typescript"
    ],
    "sourceType": "unambiguous",
    "plugins": [
        "babel-plugin-transform-typescript-metadata",
        ["@babel/plugin-proposal-decorators", { "legacy": true }],
        "@babel/plugin-proposal-class-properties",
        "@babel/plugin-transform-runtime"
    ]
  }

最佳答案

问题出在 webpack.config.js 输出部分的“library”选项,删除它让我成功运行 lambda 函数

关于node.js - 处理程序未定义或未导出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68868265/

相关文章:

javascript - 如何在 Node.js 中显示仅限管理员的链接?

javascript - 使用 SystemJS 加载模块时如何摆脱 "default"属性?

javascript - 在 Javascript 中实现 Duck Typing 时,.file() 方法不起作用

amazon-web-services - AWS服务-启用另一个区域

javascript - 使用 Twilio 将消息转发到另一个号码

javascript - 在没有窗口对象的情况下正确记录 JavaScript 中的按键事件

javascript - Json 合并左或右 Typescript

python - AWS-Lambda:如何在 python 中处理 SNS 事件

amazon-web-services - AWS Lambda 从 VPC 中访问 Secrets Manager

node.js - Create-react-app npm run build 太慢