简体   繁体   中英

max listeners problem : typescript project and serverless webpack

I am working on a cloud-based project which is built in typescript. To deploy the service we use

build : serverless webpack --stage=dev
deploy: serverless deploy --stage=dev

I am facing the following issue when functions names defined in serverless.yml are more than 10 or 11. sample function declaration in serverless

 func-abc: handler: src/api/get-user-details events: - http: authorizer: ${file(authorizer.yml)} path: /user/{user-id}/ method: GET parameters: paths: user-id: true cors: true

webpack-config.js

const slsw = require('serverless-webpack');
const nodeExternals = require('webpack-node-externals');
const ForkTsCheckerWebpackPlugin = require('fork-ts-checker-webpack-plugin');
const CopyWebpackPlugin = require('copy-webpack-plugin');

module.exports = {
    context: __dirname,
    mode: 'production',
    entry: slsw.lib.entries,
    devtool: 'source-map',
    resolve: {
        extensions: ['.mjs', '.json', '.ts'],
        symlinks: false,
        cacheWithContext: false,
    },
    output: {
        libraryTarget: 'commonjs',
        path: path.join(__dirname, '.webpack'),
        filename: '[name].js',
    },
    target: 'node',
    optimization: {
        minimize: true,
    },
    externals: [nodeExternals()],
    module: {
        rules: [
            // all files with a `.ts` or `.tsx` extension will be handled by `ts-loader`
            {
                test: /\.(tsx?)$/,
                loader: 'ts-loader',
                exclude: [
                    [
                        path.resolve(__dirname, 'node_modules'),
                        path.resolve(__dirname, '.serverless'),
                        path.resolve(__dirname, '.webpack'),
                    ],
                ],
                options: {
                    transpileOnly: true,
                    experimentalWatchApi: true,
                },
            },
        ],
    },
    plugins: [
        new ForkTsCheckerWebpackPlugin({
            eslint: true,
            eslintOptions: {
                cache: true
            }
        }),
        new CopyWebpackPlugin([
            {from: 'config/*.json', to: ''},
            {from: 'config/*.pem', to: ''},
        ])
    ],
};
(node:9972) MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 exit listeners added to [process]. Use emitter.setMaxListeners() to increase limit
(Use `node --trace-warnings ...` to show where the warning was created)

After this warning, code is not getting deployed to AWS.

How to solve this issue.

Thanks in advance.

It worked for me with below webpack.config.js :

const serverlessWebpack = require('serverless-webpack');
const webpack = require('webpack');
const nodeExternals = require('webpack-node-externals');
const TerserPlugin = require('terser-webpack-plugin');
const ForkTsCheckerWebpackPlugin = require('fork-ts-checker-webpack-plugin');

module.exports = {
  devtool: 'inline-cheap-module-source-map',
  entry: serverlessWebpack.lib.entries,
  mode: 'production',
  module: {
    rules: [{
        test: /\.ts$/,
        exclude: /node_modules/,
        loader: 'ts-loader',
        options: {
          // disable type checker - we will use it in fork plugin
          transpileOnly: true
        }
      }
    ],
  },
  node: false,
  externals: [nodeExternals()],
  optimization: {
    minimize: true,
    minimizer: [new TerserPlugin({
      terserOptions: {
        keep_classnames: true,
        keep_fnames: true,
      }
    }
    )],
  },
  resolve: {
    extensions: ['.mjs', '.ts', '.js']
  },
  plugins: [
    new ForkTsCheckerWebpackPlugin()
  ],
  target: 'node',
};

Recently I was solving the similar problem. see here for the detailed guide ( serverless+webpack )

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