簡體   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