简体   繁体   中英

webpack & aws lambda

I am trying to use Weback to build a simple lambda nodejs function 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.

Webpack generated code for the above does not work. The function throws the error "Handler 'handler' missing on module 'index'". 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.

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?

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.

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)

Here is the relevant section from Webpack guides:

libraryTarget: "commonjs" - The return value of your entry point will be assigned to the exports object using the output.library value. As the name implies, this is used in CommonJS environments.

Here is the link to that particular Webpack Guide: https://webpack.js.org/configuration/output/#expose-via-object-assignment

Here is your new 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.

Good Luck!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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