Serverless-offline: handler for 'index' is not a function'

Created on 9 Jul 2019  路  1Comment  路  Source: dherault/serverless-offline

I am using serverless-offline with serverless-webpack, but it fails to load saying that

Serverless-offline: handler for \'index\' is not a function'

I did some digging, and it appears that this line:

handler = require(funOptions.handlerPath)[funOptions.handlerName]

returns a object (a module) rather than a function. So the check for this error message passes and the error is thrown.

Below in the webpack config you can see I set the library target to commonjs which is recommended by serverless-webpack. I tried playing with different libraryTargets although they all produced the same error, but for different reasons.

Also to note, handlerName is set to js since I am importing server/controllers/web.js. If I remove output.library from my webpack config then require(funOptions.handlerPath) only has a default export. The problem here is that 'handlerName' is not 'default', but 'js'.

Am I missing some config somewhere?

Below is all the config I am using, although happy to share more of it.. I just didn't want to overwhelm.

provider:
    name: aws
    runtime: nodejs10.x
functions:
    index:
        handler: server/controllers/web.js
        events:
            - http:
                  method: GET
                  path: /
plugins:
    - serverless-webpack
    - serverless-offline
    - serverless-finch
    - serverless-scriptable-plugin

and my webpack looks a bit like this:

module.exports = {
    ...clientWebpackConfig,
    entry: slsw.lib.entries,
    output: {
        libraryTarget: 'commonjs',
        path: path.join(__dirname, '.webpack'),
        filename: '[name].js',
        library: 'js',
    },
    name: 'server',
    externals: [
        'react-helmet',
        'babel-polyfill',
        'react',
        'react-dom',
        '@loadable/component',
    ],
    cache: true,
    target: 'node',
    optimization: {
        minimize: false,
    },
}

and server/controllers/web.js looks a bit like this:

export default async function webController(event) {
    const html = renderToString(extractable)

    return {
        statusCode: 200,
        headers: {
            'Cache-Control': 'public, max-age=86400',
            'Content-Type': 'text/html',
        },
        body: webTemplate(html),
    }
}
question

Most helpful comment

Fixed by changing server/controllers/web.js to server/controllers/web.default

馃槉

>All comments

Fixed by changing server/controllers/web.js to server/controllers/web.default

馃槉

Was this page helpful?
0 / 5 - 0 ratings

Related issues

aldofunes picture aldofunes  路  3Comments

ktwbc picture ktwbc  路  4Comments

mattmeye picture mattmeye  路  4Comments

conradoramalho picture conradoramalho  路  3Comments

Looveh picture Looveh  路  4Comments