简体   繁体   English

webpack&aws lambda

[英]webpack & aws lambda

I am trying to use Weback to build a simple lambda nodejs function hello world. 我正在尝试使用Weback构建一个简单的lambda nodejs函数hello world。

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

This function works in lambda with handler "index.handler" configured in aws lambda configuration page. 此函数在lambda中工作,并在aws lambda配置页面中配置处理程序“index.handler”。

Webpack generated code for the above does not work. Webpack为上面生成的代码不起作用。 The function throws the error "Handler 'handler' missing on module 'index'". 该函数抛出模块'index'上的错误“Handler'handler'”。 It looks like module becomes antonyms. 它看起来像模块成为反义词。

It can be made to work by updating the generated code as below. 可以通过更新生成的代码使其工作,如下所示。

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 as follows. 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:
        }]
    }
}

Anyone using webpack to build lambda nodejs functions? 有谁使用webpack来构建lambda nodejs函数?

Any help appreciated. 任何帮助赞赏。

I've replicated your error and found a slight change to make it run. 我已经复制了你的错误,并发现了一个小小的改动,让它运行。

In webpack.config.js, I've added libraryTarget: 'commonjs' to the output object. 在webpack.config.js中,我将libraryTarget:'commonjs'添加到输出对象。

You need to tell webpack that this code will be run in a commonjs environment, and it will attach the entrypoint to the exports object (As Lambda expects, and as your work-around does manually) 你需要告诉webpack这个代码将在commonjs环境中运行,并且它会将入口点附加到exports对象(正如Lambda所期望的那样,并且你的解决方案是手动的)

Here is the relevant section from Webpack guides: 以下是Webpack指南中的相关部分:

libraryTarget: "commonjs" - The return value of your entry point will be assigned to the exports object using the output.library value. libraryTarget:“commonjs” - 使用output.library值将入口点的返回值分配给exports对象。 As the name implies, this is used in CommonJS environments. 顾名思义,这是在CommonJS环境中使用的。

Here is the link to that particular Webpack Guide: https://webpack.js.org/configuration/output/#expose-via-object-assignment 以下是该特定Webpack指南的链接: https ://webpack.js.org/configuration/output/#expose-via-object-assignment

Here is your new webpack.config.js 这是你的新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$/
        }]
    }
}

I've also removed that last empty attribute in your loaders array. 我还删除了你的loaders数组中的最后一个空属性。

Good Luck! 祝好运!

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

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