无法处理堆栈输出:require(...)[func] 不是函数

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

我开发serverless服务有一段时间了,一直没有搞清楚webpack文件怎么写。我一直用的是前一个或一个例子API的webpack。这对我一直有效。

现在,当我尝试将服务部署到 AWS 上时,我收到一个无服务器错误,最后显示 Cannot process Stack Output: require(...)[func] is not a function!。它已部署,但由于某种原因,API 无法正常工作。我想这可能是因为无服务器错误不允许完整部署。

下面是我的 webpak.config.js 文件。

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' },
    ],
  },
};

我在将 serverless-stack-output 插件与无服务器一起使用时出现此错误,并且仅在 util/some_handler.js.

指定自定义文件处理程序时出现此错误

导致错误的错误方式:

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

正确方法:

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

handlerutil/some_handler.js 中的一个函数,通过 module.exports:

导出
function handler(data, serverless, options) {
  // your code
}

module.exports = { handler }

serverless-stack-output docs on npm