【问题标题】:webpack & aws lambdawebpack 和 aws lambda
【发布时间】: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


【解决方案1】:

我已经复制了您的错误并发现了一些细微的变化以使其运行。

在 webpack.config.js 中,我已将 libraryTarget: 'commonjs' 添加到输出对象中。

您需要告诉 webpack 此代码将在 commonjs 环境中运行,并将入口点附加到导出对象(正如 Lambda 所期望的那样,并且您的解决方法是手动执行的)

以下是 Webpack 指南中的相关部分:

libraryTarget: "commonjs" - 入口点的返回值将使用 output.library 值分配给导出对象。顾名思义,这是在 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$/
        }]
    }
}

我还删除了加载程序数组中的最后一个空属性。

祝你好运!

【讨论】:

    猜你喜欢
    • 2018-06-02
    • 1970-01-01
    • 2018-04-26
    • 2017-01-28
    • 2017-11-04
    • 2016-06-23
    • 2020-04-20
    • 2017-01-31
    • 1970-01-01
    相关资源
    最近更新 更多