---
title: Configure Webpack in your Nx workspace
description: A guide on how to configure webpack on your Nx workspace, and instructions on how to customize your webpack configuration
sidebar:
  label: How to configure Webpack in your Nx workspace
filter: 'type:Guides'
topics:
  - 'Webpack'
---

You can configure Webpack using a [`webpack.config.js`](https://webpack.js.org/concepts/configuration/) file in your project. Nx infers the `build` and `serve` targets from your webpack configuration as long as you have `@nx/webpack/plugin` added to your `nx.json`.

```json5
// nx.json
"plugins": [
  {
    "plugin": "@nx/webpack/plugin",
    "options": {
      "buildTargetName": "build",
      "serveTargetName": "serve"
    }
  }
]
```

In the webpack config file, you can add the necessary configuration for Webpack. Read more on how to configure Webpack in the [Webpack documentation](https://webpack.js.org/concepts/configuration/).

## Basic and Nx-enhanced configuration files

Nx supported two flavors of Webpack configuration file:

1. [_Basic_](#basic-configuration-for-nx) (or standard) Webpack configuration. The file exports a Webpack config object, or one of the [standard configuration types](https://webpack.js.org/configuration/configuration-types).
2. [_Nx-enhanced_](#nx-enhanced-configuration-with-composable-plugins) Webpack configuration. The file exports a function built with `composePlugins` that takes in a Webpack configuration object plus the Nx build options and task context.

Nx v24 removed the composable plugins behind the second flavor, so write a basic configuration. It works with the Webpack CLI and with the inferred `@nx/webpack/plugin`.

### Basic configuration for Nx

A basic Webpack configuration looks like this:

```js
// apps/demo/webpack.config.js
const { NxAppWebpackPlugin } = require('@nx/webpack/app-plugin');
const { join } = require('path');

module.exports = {
  output: {
    path: join(__dirname, '../../dist/apps/demo'),
  },
  devServer: {
    port: 4200,
  },
  plugins: [
    new NxAppWebpackPlugin({
      main: './src/main.ts',
      tsConfig: './tsconfig.app.json',
      index: './src/index.html',
      styles: ['./src/styles.css'],
      outputHashing: process.env['NODE_ENV'] === 'production' ? 'all' : 'none',
      optimization: process.env['NODE_ENV'] === 'production',
    }),
  ],
};
```

The [`NxAppWebpackPlugin`](/docs/kb/webpack-plugins#nxappwebpackplugin) plugin takes a `main` entry file and produces a bundle in the output directory as defined in `output.path`. You can also pass the `index` option if it is a webapp, which will handle outputting scripts and stylesheets in the output file. `NxWebpackPlugin` is optional, and you can bring your own Webpack configuration without using it or any plugins from `@nx/webpack`.

For more information, see the [Webpack plugins guide](/docs/kb/webpack-plugins).

### Nx-enhanced configuration with composable plugins

Nx v24 removed `composePlugins`, `withNx`, `withWeb`, and `withReact`. Their exports remain as inert stubs so an unmigrated config still loads, but they no longer configure the build. A configuration written against them looked like this:

```js
// apps/demo/webpack.config.js
const { composePlugins, withNx } = require('@nx/webpack');

module.exports = composePlugins(
  // Default Nx composable plugin
  withNx(),
  // Custom composable plugin
  (config, { options, context }) => {
    // `config` is the Webpack configuration object
    // `options` holds Nx build options, such as `projectRoot`
    // `context` holds the Nx task context, such as `projectName`
    // customize configuration here
    return config;
  }
);
```

Move each composable plugin to its standard equivalent: `withNx` and `withWeb` become [`NxAppWebpackPlugin`](/docs/kb/webpack-plugins#nxappwebpackplugin), and `withReact` becomes [`NxReactWebpackPlugin`](/docs/kb/webpack-plugins#nxreactwebpackplugin). A custom callback that took `options` and `context` becomes a local webpack plugin, or plain configuration with the values supplied directly. Running `nx migrate` generates step-by-step instructions for the projects in your workspace.

## Customize your Webpack configuration

For most apps, the default configuration of Webpack is sufficient, but sometimes you need to tweak a setting in your Webpack config. The following sections show how to make a small change without taking on the maintenance burden of the entire webpack config.

### Configure Webpack for React projects

React projects use the `@nx/react` package to build their apps. This package provides `NxReactWebpackPlugin`, which adds the necessary configuration for React to work with Webpack.

```js
// apps/demo/app/webpack.config.js
const { NxAppWebpackPlugin } = require('@nx/webpack/app-plugin');
const { NxReactWebpackPlugin } = require('@nx/react/webpack-plugin');
const { join } = require('path');

module.exports = {
  output: {
    path: join(__dirname, '../../dist/apps/demo'),
  },
  devServer: {
    port: 4200,
  },
  plugins: [
    new NxAppWebpackPlugin({
      tsConfig: './tsconfig.app.json',
      compiler: 'swc',
      main: './src/main.tsx',
      index: '.src/index.html',
      styles: ['./src/styles.css'],
      outputHashing: process.env['NODE_ENV'] === 'production' ? 'all' : 'none',
      optimization: process.env['NODE_ENV'] === 'production',
    }),
    new NxReactWebpackPlugin({
      // Uncomment this line if you don't want to use SVGR
      // See: https://react-svgr.com/
      // svgr: false
    }),
  ],
};
```

### Configure Webpack for Module Federation

Nx v24 removed `withModuleFederation` and the rest of Nx Module Federation. Add `ModuleFederationPlugin` from `@module-federation/enhanced/webpack` to a standard webpack config instead. To move an existing config, follow [migrate from Nx Module Federation](/docs/kb/migrate-from-nx-module-federation) for React or [migrate Angular Module Federation](/docs/kb/migrate-angular-module-federation) for Angular.

Reference the [Webpack documentation](https://webpack.js.org/configuration/) for details on the structure of the Webpack
configuration object.

### Configure Webpack for Next.js Applications

Next.js supports Webpack customization in the `next.config.js` file.

```js
// next.config.js
/** @type {import('next').NextConfig} */
const nextConfig = {
  webpack: (
    config,
    { buildId, dev, isServer, defaultLoaders, nextRuntime, webpack }
  ) => {
    // Important: return the modified config
    return config;
  },
};

module.exports = nextConfig;
```

Read the [official documentation](https://nextjs.org/docs/api-reference/next.config.js/custom-webpack-config) for more details.
