繁体   English   中英

webpack&aws lambda

[英]webpack & aws lambda

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

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

此函数在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;

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函数?

任何帮助赞赏。

我已经复制了你的错误,并发现了一个小小的改动,让它运行。

在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$/
        }]
    }
}

我还删除了你的loaders数组中的最后一个空属性。

祝好运!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM