Cal*_*oud 3 node.js webpack aws-lambda
我正在尝试使用Weback构建一个简单的lambda nodejs函数hello world.
exports.handler = (event, context, callback) => {
callback(null, 'Hello from Lambda');
};
Run Code Online (Sandbox Code Playgroud)
此函数在lambda中工作,并在aws lambda配置页面中配置处理程序"index.handler".
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;
Run Code Online (Sandbox Code Playgroud)
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:
}]
}
}
Run Code Online (Sandbox Code Playgroud)
有谁使用webpack来构建lambda nodejs函数?
任何帮助赞赏.
小智 5
我已经复制了你的错误,并发现了一些细微的变化让它运行.
在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$/
}]
}
}
Run Code Online (Sandbox Code Playgroud)
我还删除了你的loaders数组中的最后一个空属性.
祝好运!
| 归档时间: |
|
| 查看次数: |
842 次 |
| 最近记录: |