Search code examples
webpackserverless-frameworkaws-serverless

Cannot process Stack Output: require(...)[func] is not a function


I have been developing serverless services for quite sometime, but haven't figured out how to write the webpack file. I have always used the webpack of a previous or an example API. This has always worked for me.

Now, when I am trying to deploy a service onto AWS, I get a serverless error saying Cannot process Stack Output: require(...)[func] is not a function! in the end. It gets deployed, but for some reason, the API is not working. I suppose its probably because of the serverless error which is not allowing a complete deployment.

Below is my webpak.config.js file.

const path = require('path');
const slsw = require('serverless-webpack');

const entries = {};

Object.keys(slsw.lib.entries).forEach(
  key => (entries[key] = ['./source-map-install.js', slsw.lib.entries[key]])
);

module.exports = {
  mode: slsw.lib.webpack.isLocal ? 'development' : 'production',
  entry: entries,
  devtool: 'source-map',
  resolve: {
    extensions: ['.js', '.jsx', '.json', '.ts', '.tsx'],
  },
  output: {
    libraryTarget: 'commonjs',
    path: path.join(__dirname, '.webpack'),
    filename: '[name].js',
  },
  target: 'node',
  module: {
    rules: [
      // all files with a `.ts` or `.tsx` extension will be handled by `ts-loader`
      { test: /\.tsx?$/, loader: 'ts-loader' },
    ],
  },
};


Solution

  • I got this error when I was using the serverless-stack-output plugin with serverless, and it was only when specifying a custom file handler at util/some_handler.js.

    Incorrect way that caused an error:

    custom:
      output:
        handler: util/some_handler.js # <-- incorrect: do NOT use the file extension
        file: stack-output.json
    

    Correct way:

    custom:
      output:
        handler: util/some_handler.handler # <-- note the .handler
        file: stack-output.json
    

    handler is a function in util/some_handler.js that's exported via module.exports:

    function handler(data, serverless, options) {
      // your code
    }
    
    module.exports = { handler }
    

    See the serverless-stack-output docs on npm