编程语言
首页 > 编程语言> > javascript – Webpack和AWS Lambda问题 – 模块上缺少处理程序

javascript – Webpack和AWS Lambda问题 – 模块上缺少处理程序

作者:互联网

我正在使用ES6,babel和Webpack 2捆绑AWS Lambda.然后我使用AWS SAM本地运行/测试它.当我点击api时出现以下错误 –

Handler 'handler' missing on module 'dist/main'

这是我的webpack.config.js –

const path = require('path');

module.exports = {
  entry: './index.js',
  output: {
    path: path.resolve(__dirname, 'dist'),
    filename: 'main.js',
    libraryTarget: 'commonjs'
  },
  module: {
    rules: [
      {
        test: /\.js$/,
        exclude: /node_modules/,
        loader: 'babel-loader',
        options: {
          plugins: [require('babel-plugin-transform-flow-strip-types')],
          presets: [
            [
              'env',
              {
                target: { node: 6.10 }, // Node version on AWS Lambda
                useBuiltIns: false,
                loose: false,
                exclude: [],
                debug: false
              },
            ],
          ],
        },
      }
    ],
  }
};

以下是已编译的main.js的片段 –

/***/ (function(module, exports, __webpack_require__) {

"use strict";

Object.defineProperty(exports, "__esModule", {
    value: true
});
exports.handler = handler;

var _amazonCognitoIdentityJs = __webpack_require__(60);

var _aws_profile = __webpack_require__(290);

// A signin Lambda function
function handler(event, context, callback) {
        switch (event.httpMethod) {
        case "GET":

一点背景……这是一个Lambda,我最初在ES6中写道并没有使用Webpack进行捆绑,而且它正在工作.我现在需要它在ES6中并与Webpack一起工作.注:这是Webpack 2

非常感谢…

解决方法:

要解决此问题,我必须指定一个库属性并将libraryTarget更改为commonjs2. webpack.config.js文件输出现在看起来像这样 –

output: {
    path: path.resolve(__dirname, 'dist'),
    filename: 'main.js',
    library: 'main',
    libraryTarget: 'commonjs2'
  },

标签:javascript,lambda,amazon-web-services,webpack,webpack-2
来源: https://codeday.me/bug/20190607/1195719.html