【发布时间】:2018-03-08 20:28:51
【问题描述】:
我正在尝试使用 Weback 构建一个简单的 lambda nodejs 函数 hello world。
exports.handler = (event, context, callback) => {
callback(null, 'Hello from Lambda');
};
此函数在 lambda 中使用,并且在 aws lambda 配置页面中配置了处理程序“index.handler”。
上面的 Webpack 生成的代码不起作用。该函数抛出错误“模块'索引'上缺少处理程序'处理程序'”。看起来模块变成了反义词。
可以通过如下更新生成的代码来使其工作。
global.handler = (event, context, callback) => {
//async.map(['file1','file2','file3'], console.log, function(err, results){
// results is now an array of stats for each file
callback(null, 'Hello from Lambda');
//});
//add the following at the end.
exports.handler = global.handler;
webpack.config.js 如下。
var path = require('path');
module.exports = {
// Specify the entry point for our app.
entry: [
path.join(__dirname, '/src/autotag.js')
],
// Specify the output file containing our bundled code
output: {
path: path.join(__dirname, "dist"),
filename: "autotag.js"
},
//target: "node",
module: {
/**
* Tell webpack how to load 'json' files.
* When webpack encounters a 'require()' statement
* where a 'json' file is being imported, it will use
* the json-loader.
*/
loaders: [{
test: /\.json$/,
loaders:
}]
}
}
有人使用 webpack 构建 lambda nodejs 函数吗?
任何帮助表示赞赏。
【问题讨论】:
-
为什么你的处理程序命名为“index.handler”,而你在webpack中的入口点是“autotag.js”?你可以包含你的目录结构来显示文件的相对位置吗?
-
一个简单的解决方案是简单地编写一个脚本,将
exports.handler = global.handler;附加到最终包的末尾。我通常写npm run bundle首先运行webpack,然后再运行一个脚本,将其附加到包的末尾。 -
感谢您的调查。 Webpack 在 dist 文件夹中生成 autotag.js 文件。然后将代码复制到 AWS Lambda 以创建文件。对于任何内联代码,处理程序都是“index.handler”。我也尝试过在根文件夹中使用包含 autotag.js 的 zip 文件以及 node_modules 但同样的错误。在这种情况下,处理程序是 autotag.hander。
标签: node.js webpack aws-lambda