serverless-webpack

raw JSON →
5.15.4 verified Sat Apr 25 auth: no javascript

A Serverless Framework plugin that bundles Lambda function code using Webpack, enabling tree-shaking, individual packaging, Babel transpilation, and custom loaders. Current stable version is 5.15.4, released April 2025. Requires Node.js >= 20 and supports Serverless v1 through v4 (with esbuild disabled in v4). Active development with monthly releases. Compared to serverless-bundle or serverless-esbuild, this plugin offers broader Webpack version support (3, 4, 5), NPM/Yarn packaging, async webpack config, and seamless integration with serverless-offline.

error Error: Cannot find module 'serverless-webpack'
cause Plugin not installed or not in devDependencies.
fix
Run npm install serverless-webpack --save-dev.
error Configuration error: Cannot resolve 'webpack'
cause Missing webpack as a devDependency.
fix
Run npm install webpack --save-dev.
error TypeError: slsw.lib.serverless.providers.aws.getAccountId is not a function
cause Using async webpack config outside of Lambda edge context.
fix
Ensure you are using the correct slsw object from the plugin; check serverless version compatibility.
error Error: spawn EINVAL
cause Node > 20.12.2 on Windows, older serverless-webpack version.
fix
Upgrade serverless-webpack to v5.14.0+.
breaking Node.js >= 20 required as of v5.15.4. Node 18 dropped in v5.15.4, Node 16 dropped in v5.14.0.
fix Upgrade Node.js to version 20 or later.
breaking Serverless v4 requires disabling built-in esbuild: set build.esbuild: false in serverless.yml.
fix Add build: esbuild: false to serverless.yml.
breaking Removed is-builtin-module dependency in v5.15.4. Might break if relying on that polyfill.
fix Ensure your code does not depend on is-builtin-module being available.
deprecated Serverless v1-v3 support may be dropped in future releases. The peer dependency allows v4.
fix Migrate to Serverless v4 if needed.
gotcha Native ESM output requires copyPackageSectionNames config and may need package.json adjustments.
fix Use copyPackageSectionNames in custom.webpack and ensure package.json has required fields.
gotcha E2E tests and some features may fail on Windows due to spawn EINVAL on Node > 20.12.2 (fixed in v5.14.0).
fix Upgrade to v5.14.0+ or use Node <= 20.12.2 on Windows.
gotcha When using serverless-offline, webpack config must not have mode: 'production' to allow source maps.
fix Use mode: 'development' or set devtool appropriately for local runs.
npm install serverless-webpack
yarn add serverless-webpack
pnpm add serverless-webpack

Initialize a Serverless service with serverless-webpack, install plugin, configure webpack for Node.js target, and create a basic handler.

npm install serverless-webpack --save-dev

# serverless.yml
service: my-service
plugins:
  - serverless-webpack
provider:
  name: aws
  runtime: nodejs20.x
custom:
  webpack:
    webpackConfig: 'webpack.config.js'
    includeModules: false
functions:
  hello:
    handler: handler.hello

// webpack.config.js
const path = require('path');
module.exports = {
  entry: './handler.js',
  target: 'node',
  output: {
    libraryTarget: 'commonjs2',
    path: path.join(__dirname, '.webpack'),
    filename: '[name].js',
  },
};

// handler.js
exports.hello = async (event) => {
  return { statusCode: 200, body: 'Hello from serverless-webpack!' };
};