[英]Webpack and AWS Lambda issue - handler missing on module
I'm using ES6, babel and Webpack 2 to bundle an AWS Lambda. 我正在使用ES6,babel和Webpack 2捆绑AWS Lambda。 I am then running/testing it using AWS SAM local. 然后我使用AWS SAM本地运行/测试它。 I get the following error when I hit the api - 当我点击api时出现以下错误 -
Handler 'handler' missing on module 'dist/main'
Here is my webpack.config.js - 这是我的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
},
],
],
},
}
],
}
};
And here is a snippet of the compiled main.js - 以下是已编译的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":
A little background.... this is a Lambda I initially wrote NOT in ES6 and not bundling using Webpack and it was working. 一点背景......这是一个Lambda,我最初在ES6中写道并没有使用Webpack进行捆绑,而且它正在工作。 I now need for it to be in ES6 and work with Webpack. 我现在需要它在ES6中并与Webpack一起工作。 NB this is Webpack 2 注意这是Webpack 2
Much thanks... 非常感谢...
To fix this issue I had to specify a library property and change the libraryTarget to commonjs2. 要解决此问题,我必须指定一个库属性并将libraryTarget更改为commonjs2。 The webpack.config.js file output now looks like this - webpack.config.js文件输出现在看起来像这样 -
output: {
path: path.resolve(__dirname, 'dist'),
filename: 'main.js',
library: 'main',
libraryTarget: 'commonjs2'
},
I ran into this issue as well. 我也遇到过这个问题。 However, I believe my situation was the inverse or what SamBrick shares. 但是,我相信我的情况与SamBrick分享的相反。 I was moving from transpiling ES6 with babel to run on lambda/node 6.10 to no transpiling and targeting lambda/node 8.10. 我正在从使用babel转换ES6到在lambda / node 6.10上运行到没有转换并且针对lambda /节点8.10。 Removing the library
field and changing the libraryTarget: 'commonjs'
solved the problem for me. 删除library
字段并更改libraryTarget: 'commonjs'
解决了我的问题。
Props to this guy: https://gist.github.com/nirnanaaa/d7f40deb38f1cf7f931dc7ef0c582bf0 这个家伙的道具: https : //gist.github.com/nirnanaaa/d7f40deb38f1cf7f931dc7ef0c582bf0
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.