You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When I compile my website with webpack, the target is to use Defaults to browserslist, and it compile my web worker use its exports "browser": "./index.dom.js", the error happens ReferenceError: document is not defined.
If the current behavior is a bug, please provide the steps to reproduce.
What is the expected behavior?
How can I set my web worker's target to webworker separately?
I want to compile the package named decode-named-character-reference use its exports "worker": "./index.js", then it can work well.
Other relevant information:
webpack version: 5.73.0
Node.js version: 14.20.0
Operating System: macOS Big Sur
Additional tools: VS code
The worker is a .ts file that have import remark-stringify that is eventually import decode-named-character-reference and its package.json contains the following
WebWorker NotesImporter.worker.ts will be resolved via standard webpack rules
Any imports in file NotesImporter.worker.ts or its transitive imports will use config conditionNames: ['worker', 'import', 'module', 'default'] so package decode-named-character-reference will be resolved as decode-named-character-reference/index.js
The actual behavior is:
Code for main thread builds well
The worker code throws error ReferenceError: document is not defined
For now, you can work around this with layers: mark the worker's graph root via a dependency: "worker" rule, let the layer inherit transitively, and resolve that layer with the worker condition first.
I also considered whether this belongs in webpack core. On closer look, the semantics of an automatic fix are more problematic than they seem.
module.exports={// ...module: {rules: [// module created by `new Worker(new URL(...))` starts the worker layer{dependency: "worker",layer: "worker",resolve: {conditionNames: ["worker","..."]}},// everything transitively reached from it stays in that layer{issuerLayer: "worker",resolve: {conditionNames: ["worker","..."]}}]}};
We compared this exact scenario across bundlers (browser-target build, worker importing a package with worker/browser conditional exports, both directly and transitively):
rspack — bundles the worker into a separate chunk, but resolves its imports with the browser condition (same behavior as webpack).
turbopack — bundles the worker into a separate chunk group, also with the browser condition.
esbuild — doesn't bundle new Worker(new URL(...)) at all; the expression is left as-is in the output.
rolldown — same as esbuild, the worker request is not bundled.
Activity
[-]Unable to set the target for Web Workers[/-][+]Unable to set target for Web Workers[/+]webpack-bot commentedon Oct 19, 2023
This issue had no activity for at least three months.
It's subject to automatic issue closing if there is no activity in the next 15 days.
webpack-bot commentedon Nov 3, 2023
Issue was closed because of inactivity.
If you think this is still a valid issue, please file a new issue with additional information.
bastotec commentedon Apr 7, 2024
I'm facing the same issue. Anyone else? Any workarounds?
8 remaining items
SurendraKumawat7742 commentedon Apr 4, 2025
I want to resolve this problem.
morijvan commentedon Oct 6, 2025
Hi I am interested to work on this particular issue can you please assign this issue.
vitonsky commentedon May 13, 2026
Hello everyone, what's progress on this?
I see some activity related to that issue:
The current state
I just faced with the same problem on my project.
I use latest webpack version, I have webpack config that build
target: 'web'.My
.tsfile have codeThe worker is a
.tsfile that have importremark-stringifythat is eventually importdecode-named-character-referenceand its package.json contains the followingIn our webpack config we have
so my expectations is:
NotesImporter.worker.tswill be resolved via standard webpack rulesNotesImporter.worker.tsor its transitive imports will use configconditionNames: ['worker', 'import', 'module', 'default']so packagedecode-named-character-referencewill be resolved asdecode-named-character-reference/index.jsThe actual behavior is:
ReferenceError: document is not definedI've also tried update webpack config to
It completely break our app:
The question
I just want to know about progress on this problem.
If I miss something or you think I understand something wrong, please do correct me.
alexander-akait commentedon May 13, 2026
Please create reproducible test repo, it should work in most of cases, but yeah we still working on it, thanks
hai-x commentedon Jul 13, 2026
For now, you can work around this with layers: mark the worker's graph root via a
dependency: "worker"rule, let the layer inherit transitively, and resolve that layer with the worker condition first.I also considered whether this belongs in
webpackcore. On closer look, the semantics of an automatic fix are more problematic than they seem.We compared this exact scenario across bundlers (browser-target build, worker importing a package with worker/browser conditional exports, both directly and transitively):