Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Webpack still including an explicitly excluded folder

I'm using webpack on a nodeJs repo with the serverless.com framework for deploying to AWS Lambda functions. Since the Lambda container on AWS already contains aws-sdk, I'm attempting to exclude it from packaging.

The output during the build in fact identifies aws-sdk as excluded:

    [41] ./src/controller/queue.ts 2.57 KiB {3} [built]
[43] ./src/controller/auth.ts 3.76 KiB {1} [built]
    + 30 hidden modules
Serverless: Excluding external modules: aws-sdk@^2.309.0
Serverless: Package lock found - Using locked versions
Serverless: Packing external modules: @hapi/joi@^16.1.5, moment-timezone@^0.5.23, axios@^0.19.0, chrome-aws-lambda@^2.0.1, puppeteer-core@^2.0.0, uuid@^2.0.3, memorystream@^0.3.1, lambda-warmer@^1.2.1, jsonwebtoken@^8.5.1, [email protected], js-yaml@^3.12.0
Serverless: Packaging service...

Yet the zip file which is built to be uploaded to Lambda still contains aws-sdk and is making the package almost 60MB. I also watched the folders in .webpack during the build process and noticed that the node_modules folder is in both a dependencies folder and a service folder and aws-sdk is under the service folder. I don't really know what that means though.

Any help is appreciated. It's frustrating that it blatantly says it's excluding it yet it winds up in the zip anyway.

My webpack.config.js is below:

const webpack = require("webpack");
const path = require("path");
const serverlessWebpack = require("serverless-webpack");
const nodeExternals = require("webpack-node-externals");
const CopyWebpackPlugin = require('copy-webpack-plugin');

module.exports = {
  entry: serverlessWebpack.lib.entries,
  target: 'node',
  mode: serverlessWebpack.lib.webpack.isLocal ? "development" : "production",
  node: {
    __dirname: true
  },
  devtool: 'source-map',
  externals: [nodeExternals()],
  module: {
    rules: [
      {
        test: /\.ts$/,
        use: [
          {
            loader: "ts-loader"
          }
        ],
        include: [__dirname],
        exclude: /node_modules/
      }
    ]
  },
  resolve: {
    extensions: [".ts", ".tsx", ".js"]
  },
  output: {
    libraryTarget: "commonjs",
    path: path.join(__dirname, ".webpack"),
    filename: "[name].js"
  },
  plugins: [
    new webpack.EnvironmentPlugin({
      NODE_ENV: "development"
    }),
    new CopyWebpackPlugin([
      { from: 'ormconfig.yml' },
    ])
  ]
};

And in my serverless.yml custom section for the serverless-webpack plugin, my config is:

webpack:
  webpackConfig: webpack.config.js
  includeModules:
    forceExclude:
    - aws-sdk
  packager: 'yarn'

like image 542
Kris White Avatar asked Sep 06 '25 03:09

Kris White


2 Answers

I have the same problem and I don't understand why aws-sdk is still included in the deployment package, which makes about 50MB! My solution is

webpack:
webpackConfig: webpack.config.js   # Name of webpack configuration file
packager: 'yarn'
includeModules:
  forceExclude:
    - aws-sdk
packagerOptions:
  scripts:
    - rm -rf node_modules/aws-sdk
like image 182
Crespo Wang Avatar answered Sep 07 '25 21:09

Crespo Wang


do you have an indentation issue. see forceExclude and includeModules suppose to be in the same level?

should it be,

webpack:
  webpackConfig: webpack.config.js
  includeModules: true
  forceExclude:
    - aws-sdk
  packager: 'yarn'

Reference:

https://github.com/serverless-heaven/serverless-webpack

like image 33
Arun Kamalanathan Avatar answered Sep 07 '25 20:09

Arun Kamalanathan