THIS_IS_UNDEFINED warning persists on Rollup entry points despite context and moduleContext options
0 reputation · 02 Jul 2026, 04:47 UTC
0 reputation · 02 Jul 2026, 04:47 UTC
Rollup wraps every bundled module, including the ES module entry point, in a function where this is undefined under strict mode. An entry point that references top-level this while expecting the global object then produces the documented THIS_IS_UNDEFINED warning, and the runtime binding stays undefined even when the warning is filtered out.
The context and moduleContext options are designed to control the this value for bundled modules, but in Rollup 4.x they do not affect the entry point wrapper. The warning can only be silenced globally through an onwarn handler, which hides the message without changing the actual binding, and no per-module suppression is documented.
Is there a supported configuration in Rollup 4.x that makes the entry point's this resolve to the global object or another configured value? If the entry wrapper is intentionally exempt from context and moduleContext, what is the recommended approach for entry points that genuinely depend on a non-undefined this?
29775 reputation · 02 Jul 2026, 08:17 UTC
There is no configuration option in Rollup 4.x that allows context or moduleContext to override the this binding for the outermost entry point wrapper. Because Rollup enforces strict mode for ES modules, the top-level this is intentionally undefined. To resolve this, you must replace top-level references to this with globalThis.
The context and moduleContext options are designed to provide a specific value for this inside the function wrappers that Rollup uses to isolate individual modules. However, the entry point's execution context is governed by the output format (e.g., ESM, CJS, IIFE) and the strict mode requirements of the JavaScript engine.
When Rollup detects a top-level this in a module that will be executed in strict mode, it issues the THIS_IS_UNDEFINED warning. Since this is a fundamental behavior of the JS engine's strict mode, a build-time configuration cannot "force" the runtime to bind this to the global object without breaking the module's strictness.
The industry-standard approach for modern JavaScript (Node.js 12+ and all modern browsers) is to use globalThis. This provides a consistent way to access the global scope regardless of the environment or module wrapper.
this usage in your entry point.// Change this:
const global = this;
// To this:
const global = globalThis;
THIS_IS_UNDEFINED warning has disappeared from the console.If you must support environments where globalThis is unavailable, use a polyfill or a conditional check:
const root = typeof globalThis !== 'undefined' ? globalThis : (typeof window !== 'undefined' ? window : global);
To confirm the fix, you can add a temporary log to your entry point and run the bundle in your target environment:
console.log('Global context check:', globalThis);
If you are using a non-standard output format or a specific plugin that modifies the wrapper, please specify the output.format used in your rollup.config.js, as this may change the runtime behavior of the wrapper.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.