webpack&aws lambda

我正在尝试使用Weback来构build一个简单的lambda nodejs函数hello world。

exports.handler = (event, context, callback) => { callback(null, 'Hello from Lambda'); }; 

该函数在lambdaexpression式中工作,处理器“index.handler”在aws lambdaconfiguration页面中configuration。

上面的Webpack生成的代码不起作用。 该函数在模块“index”上抛出错误“Handler'handler'”。 它看起来像模块成为反义词。

可以通过如下更新生成的代码来工作。

 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构buildlambda nodejs函数的人?

任何帮助赞赏。

我已经复制了你的错误,并发现有一点变化,使其运行。

在webpack.config.js中,我添加了libraryTarget:“commonjs”到输出对象。

你需要告诉webpack这个代码将运行在一个commonjs环境中,并且它将把入口点附加到exports对象上(正如Lambda期望的那样,而且你的解决方法是手动的)

这里是Webpack指南的相关部分:

libraryTarget:“commonjs” – 使用output.library值将您的入口点的返回值分配给exports对象。 顾名思义,这是在CommonJS环境中使用的。

这里是链接到特定的Webpack指南: https ://webpack.js.org/configuration/output/#expose-via-object-assignment

这是你的新的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", libraryTarget: 'commonjs' }, //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$/ }] } } 

我也删除了你的加载器数组中的最后一个空属性。

祝你好运!