rollup.JS and "'this' keyword is equivalent to 'undefined'

The reason this is happening is that typescript will attempt to shim out await if the language target is too low, and typescript's await shim leans on global this. The warning is triggered by the use of global this, which it can't prove was safely stubbed out (but was.)

Whereas you can cause the error message to be omitted, since you're bundling, the better choice is to use a language target that presumes await.

In your tsconfig.json, set target to es2017 or higher, and this will go away.


You could use the context option and set it to this: it avoids the rewrite of this to undefined (in fact this is rewritten to... this).

See Rollup documentation:

  • https://rollupjs.org/guide/en/#error-this-is-undefined
  • https://rollupjs.org/guide/en/#context

You could safely ignore those warnings as explained in the documentation by adding the onwarn property to your rollup-config.js file:

onwarn: function(warning) {
    // Skip certain warnings

    // should intercept ... but doesn't in some rollup versions
    if ( warning.code === 'THIS_IS_UNDEFINED' ) { return; }

    // console.warn everything else
    console.warn( warning.message );
}

Quote:

It overrides the default onwarn method in order to skip annoying messages about the AOT compiler's use of the this keyword.