Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

webpack stops because two files want the same output name

ErrorConflict: Multiple assets emit to the same filename

Short answer

webpack writes every bundle into output.path. If two bundles get the same name there, one would overwrite the other, so it stops. A fixed output.filename is the usual reason once you have more than one entry or chunk. Give each file a unique name with [name] or [contenthash], or give each HTML plugin its own filename.

  • Applies to webpack 5, webpack-cli 6, Node.js 22, html-webpack-plugin 5
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Read the filename in the error, then look at how output.filename and entry are set in your config. A name with no [name] in it cannot be shared by two bundles.

    grep -n -E "filename|entry|HtmlWebpackPlugin" webpack.config.js
  2. 2Match what you see, then jump to its fix

    1. 1The conflicting name is the literal text from output.filename, for example bundle.jsA fixed output.filename with more than one entryFix
    2. 2The error mentions chunks (not entries) and appears after you add import() or splitChunksCode splitting creates chunks that share the static nameFix
    3. 3The conflicting name is index.htmlTwo HTML plugin instances write the same fileFix

What does the error look like?

  • Conflict: Multiple assets emit to the same filename
  • Conflict: Multiple assets emit different content to the same filename main.js

Cause 1 of 3

A fixed output.filename with more than one entry

1Check

Look for an entry object with several keys, or an array of configs, while output.filename is a plain string.

grep -n -A4 "entry" webpack.config.js
grep -n "filename" webpack.config.js

2Fix

Add [name] to the filename so each entry gets its own file. Add [contenthash] too if the files are cached by browsers.

output: {
  path: path.resolve(__dirname, 'dist'),
  filename: '[name].[contenthash].js',
},

3Verify

Run the build again and list the output folder: one file per entry.

npx webpack
ls dist

Confirmed by webpack.js.org

Did this fix it?

Cause 2 of 3

Code splitting creates chunks that share the static name

1Check

A static output.filename can only describe one file, but dynamic import() and optimization.splitChunks add more chunks. Check whether the config uses either.

grep -n -E "splitChunks|import\(" webpack.config.js src -r

2Fix

Use [name] or [contenthash] in output.filename.

output: {
  filename: '[name].[contenthash].js',
},

3Verify

Build again: the error is gone and the chunks have different names in dist.

npx webpack
ls dist

Confirmed by webpack.js.org

Did this fix it?

Cause 3 of 3

Two HTML plugin instances write the same file

1Check

html-webpack-plugin writes index.html unless you give it another filename. Two new HtmlWebpackPlugin() calls both target index.html.

grep -n -B1 -A4 "new HtmlWebpackPlugin" webpack.config.js

2Fix

Give each instance its own filename and choose which entries it includes with chunks.

plugins: [
  new HtmlWebpackPlugin({ filename: 'index.html', chunks: ['app'] }),
  new HtmlWebpackPlugin({ filename: 'admin.html', chunks: ['admin'] }),
],

3Verify

After the build, both pages exist in the output folder.

npx webpack
ls dist/*.html

Confirmed by raw.githubusercontent.com

Did this fix it?

How do I stop it coming back?

  • Put [name] in output.filename from the first day, even with one entry.
  • Use [contenthash] for production file names so browsers can cache them safely.
  • Give every HtmlWebpackPlugin instance an explicit filename.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Back end

ng serve says it cannot determine the project or target

Cannot determine project for command.

The Angular CLI looked through angular.json for a project that defines the target you asked for (serve, build, test) and found none, or found several and could not pick one.

3 causes, each with a fix

Back end

ng serve cannot find the dev-server builder package

Could not find the implementation for builder @angular/build:dev-server

The builder named in angular.json (for example @angular-devkit/build-angular:dev-server) is looked up as an npm package starting from the workspace root.

3 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.