webpack stops because two files want the same output name
Conflict: Multiple assets emit to the same filenameShort 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.
Fix it fast
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.js2Match what you see, then jump to its fix
- 1
The conflicting name is the literal text from output.filename, for example bundle.jsA fixed output.filename with more than one entryFix - 2
The error mentions chunks (not entries) and appears after you add import() or splitChunksCode splitting creates chunks that share the static nameFix - 3
The conflicting name is index.htmlTwo HTML plugin instances write the same fileFix
- 1
What does the error look like?
Conflict: Multiple assets emit to the same filenameConflict: 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.js2Fix
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 distConfirmed by webpack.js.org
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 -r2Fix
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 distConfirmed by webpack.js.org
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.js2Fix
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/*.htmlConfirmed by raw.githubusercontent.com
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 fixBack end
ng fails with Cannot find module @angular/cli/bin/ng
Cannot find module '@angular/cli/bin/ng'
The ng command is the entry point to the Angular CLI package.
2 causes, each with a fixBack end
Angular app shows 'Cannot GET /' when served by a Node.js server
Cannot GET /dashboard
Cannot GET / is Express saying no route or file matches the request.
3 causes, each with a fixBack 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 fixBack end
Angular build says it cannot resolve '@angular/localize/init'
Can't resolve '@angular/localize/init' in '/usr/src/app/src'
The build imports @angular/localize, and that package is not in node_modules.
2 causes, each with a fixBack end
The command npm run build -- --prod exited with code 1 (Angular)
The command "npm run build -- --prod" exited with code 1
Exit code 1 only says the build failed; the real message is above it.
2 causes, each with a fix