我使用 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 - Webpack 和 AWS Lambda 问题 - 模块上缺少处理程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47269383/