question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Uncaught TypeError: Failed to construct 'HTMLElement': Please use the 'new' operator, this DOM object constructor cannot be called as a function.

See original GitHub issue

Description

We are upgrading from Lit-Element v1 to Lit v2.0.2 and a lot of downstream teams are consuming our pre-existing web components which were built using v1. Post upgrade a common issue that we are facing against most of components are :

VM388:2 Uncaught TypeError: Failed to construct ‘HTMLElement’: Please use the ‘new’ operator, this DOM object constructor cannot be called as a function.

The snippet of the current tsconfig.json setup looks like this

 "composite": true,
 "target": "es2015",
 "module": "es2015",
 "lib": ["es2020", "DOM", "DOM.Iterable"],
 "declaration": true,

I am not sure what is the root-cause of this issue. Does it have something to do with the build target not being set properly. However, I might add, that some teams who use our components in their projects might run Babel to transpile their code and as a result additionally affect our components. Would Babel transpilation issues be one of the causes to this issue ?

I am providing you the area which I suspect is throwing the error. The files were minified and we do not happen to have access to the actual sources or build configurations.

       hasChanged: b
                }, e = function() {
                    var d = HTMLElement.call(this) || this;
                    d._$Et = new Map;
                    d.isUpdatePending = !1;
                    d.hasUpdated = !1;
                    d._$Ei = null;
                    d.o();
                    return d
                };
                $jscomp.inherits(e, HTMLElement);

and I suspect this transpiled code is from this source

  class o extends HTMLElement {
        constructor() {
            super(),
            this._$Et = new Map,
            this.isUpdatePending = !1,
            this.hasUpdated = !1,
            this._$Ei = null,
            this.o()
        }

Can any one help what might be causing this issue ?

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
justinfagnanicommented, Feb 23, 2022

Custom elements need to be actual classes with a real super() call, but this hasn’t changed in Lit 2 so a Lit 2 upgrade shouldn’t change anything here.

If teams are compiling to ES5 (they should stop that!) then they’ll need the custom-elements-es5-adapter: https://www.npmjs.com/package/@webcomponents/webcomponentsjs#user-content-custom-elements-es5-adapterjs

This is a shim that takes as ES5 “class” as output by Babel and wraps it in a real JS class for the browser. You must only load it on browsers with native custom elements, and be sure to not compile it.

0reactions
maharshiduttacommented, Mar 3, 2022

We had earlier used a shim to support ES5 for Lit. (Lit-Element to be specific). Somehow we had missed out adding that adaptor. But now after adding the shim. It is working at our end. Thanks for the response. Closing this issue.

Read more comments on GitHub >

github_iconTop Results From Across the Web

[Bug] angular/elements: Failed to construct 'HTMLElement ...
Uncaught TypeError : Failed to construct 'HTMLElement': Please use the 'new' operator, this DOM object constructor cannot be called as a ...
Read more >
extending HTMLElement: Constructor fails when webpack ...
Uncaught TypeError : Failed to construct 'HTMLElement': Please use the 'new' operator, this DOM object constructor cannot be called as a ...
Read more >
Chrome Browser Custom Element Error | by Allen Kim - Medium
Uncaught TypeError : Failed to construct 'HTMLElement': Please use the 'new' operator, this DOM object constructor cannot be called as a function ......
Read more >
compiling custom elements - Google Groups
... raise Uncaught TypeError: Failed to construct 'HTMLElement': Please use the 'new' operator, this DOM object constructor cannot be called as a function....
Read more >
Error on ion-router ionic 6 with react app
TypeError : Failed to construct 'HTMLElement': Please use the 'new' operator, this DOM object constructor cannot be called as a function.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found