Uncaught TypeError: Class constructor cannot be invoked without 'new'
  • 29-May-2023
Lightrun Team
Author Lightrun Team
Share
Uncaught TypeError: Class constructor cannot be invoked without 'new'

Uncaught TypeError: Class constructor cannot be invoked without ‘new’

Lightrun Team
Lightrun Team
29-May-2023

Explanation of the problem

I am currently in the process of transitioning from using polymer build to utilizing a gulpfile based on the one provided by the PolymerElements/generator-polymer-init-custom-build repository (https://github.com/PolymerElements/generator-polymer-init-custom-build). The purpose of this transition is to incorporate a version string into my application.

My application has a slightly unconventional structure. It consists of an index.html file that serves as the initial page, primarily displaying a logo. Upon window load, the following actions take place: (a) registration of a service worker, (b) addition of an event listener for the “WebComponentsReady” event, which then loads the my-app component, and (c) insertion of a script tag for webcomponents-loader.js. Within my-app, there are several lazy-loaded elements implemented using lazy-imports-mixin.html.

To achieve the desired functionality, I have customized the gulpfile based on the custom psk build gulpfile, addressing certain issues outlined in the following GitHub issue: https://github.com/PolymerElements/generator-polymer-init-custom-build/issues/71. Specifically, I have disabled the bundling process and incorporated code to add a version number. These modifications, however, do not appear to have a significant impact on the issue at hand.

Upon inspection of the console logs, I have observed that the loading process occurs in the correct order until the my-app component is loaded. At this point, I encounter the following error: “Uncaught TypeError: Class constructor <component-name> cannot be invoked without ‘new'”. This issue seems to resemble a problem described by @rictic in the webcomponents-es5-loader.js context, but I do not possess that file within my webcomponentsjs directory. I have also come across a similar error message in the following Stack Overflow post: https://stackoverflow.com/questions/43520535/class-constructor-polymerelement-cannot-be-invoked-without-new. It is unclear whether this problem is identical to the one I am experiencing.

Troubleshooting with the Lightrun Developer Observability Platform

Getting a sense of what’s actually happening inside a live application is a frustrating experience, one that relies mostly on querying and observing whatever logs were written during development.
Lightrun is a Developer Observability Platform, allowing developers to add telemetry to live applications in real-time, on-demand, and right from the IDE.

  • Instantly add logs to, set metrics in, and take snapshots of live applications
  • Insights delivered straight to your IDE or CLI
  • Works where you do: dev, QA, staging, CI/CD, and production

Start for free today

Problem solution for Uncaught TypeError: Class constructor cannot be invoked without ‘new’

The issue at hand seems to be related to using a webpack build with Babel for a Polymer web component and encountering problems when configuring Babel to target browsers other than Chrome. Specifically, when the Babel configuration includes multiple browser targets, such as Chrome, Firefox, Safari, and Internet Explorer 11, the mentioned error arises. However, when the Babel configuration is narrowed down to only target Chrome, the issue is resolved.

It is important to note that this aggregated answer is based on the provided answers and may not cover all possible scenarios or solutions. Additionally, please be aware that this issue might have been marked as stale and closed due to a lack of recent activity.

 

Other popular problems with firebase-tools

Problem: Authentication Token Expiration

One common issue encountered when using the Firebase CLI tool (firebase-tools) is the expiration of authentication tokens. When performing operations that require authentication, such as deploying Firebase functions or accessing Firestore data, the CLI tool relies on a token to authenticate the user. However, these tokens have a limited lifespan and can expire, causing authentication failures and preventing users from executing commands successfully.

Solution:

To address this problem, a solution is to refresh the authentication token before it expires. This can be achieved by using the Firebase Admin SDK to generate a new token programmatically and update the CLI tool with the refreshed token. Here’s an example of how to refresh the token using Node.js:

const admin = require('firebase-admin');

// Initialize the Firebase Admin SDK
admin.initializeApp();

// Get the current authentication token
const currentToken = admin.auth().createCustomToken(uid);

// Refresh the token and update the CLI tool
admin.auth().createCustomToken(uid)
  .then(refreshedToken => {
    // Update the CLI tool with the refreshed token
    // ...
  })
  .catch(error => {
    // Handle error refreshing the token
    // ...
  });

Problem: Slow Deployment Times

Another significant issue that users may encounter with firebase-tools is slow deployment times. When deploying Firebase functions or hosting content, the CLI tool may take an extended period to complete the deployment process, leading to frustration and delays in the development workflow.

Solution:

To improve deployment times, optimizing the deployment process is crucial. One approach is to minimize the number of files and directories included in the deployment by utilizing the Firebase .ignore file. This file allows users to specify patterns of files and directories to exclude from deployment, reducing the deployment payload size and improving the deployment speed. Here’s an example of a .ignore file:

# Exclude all files and directories in the "temp" folder
temp/

# Exclude all files with the ".log" extension
*.log

Problem:Lack of Environment-specific Configuration

Many developers face challenges when managing environment-specific configuration settings with firebase-tools. For instance, when deploying functions or hosting projects to different environments (e.g., development, staging, production), it becomes necessary to dynamically adjust configuration values such as API keys, database URLs, or third-party service credentials.

Solution:

To address this issue, one approach is to leverage environment variables in conjunction with the Firebase CLI tool. Environment variables allow developers to define and manage configuration settings specific to each environment without hardcoding them into the codebase. The Firebase CLI provides the ability to set environment variables during deployment. Here’s an example of setting environment variables using the CLI:

firebase functions:config:set env.api_key="YOUR_API_KEY" env.database_url="YOUR_DATABASE_URL"

A brief introduction to firebase-tools

Firebase Tools, also known as firebase-tools, is a powerful command-line interface (CLI) tool provided by Firebase that facilitates the development, deployment, and management of Firebase projects. It offers a wide range of features and commands that enable developers to interact with Firebase services, such as Firebase Authentication, Cloud Firestore, Cloud Functions, Hosting, and more.

One key aspect of firebase-tools is its capability to initialize and configure Firebase projects. By running commands like firebase init, developers can easily set up their project, select the desired Firebase services, and generate the necessary configuration files. This simplifies the initial project setup process and ensures that the project is properly linked to the Firebase backend.

In addition to project setup, firebase-tools provides commands for local development and testing. Developers can run their Firebase functions locally using the firebase emulators:start command, which sets up a local server to emulate Firebase services. This enables developers to test their functions and database interactions without making actual API calls to the Firebase servers. The CLI tool also offers features like Firestore data import and export, enabling developers to manage data locally and perform backups or migrations easily.

Furthermore, firebase-tools offers seamless deployment capabilities. With commands such as firebase deploy, developers can easily deploy their Firebase functions, hosting content, and other resources to the Firebase servers. The tool handles the deployment process, including packaging the code, uploading it to the servers, and configuring the necessary infrastructure. It also provides options to target specific environments, allowing developers to deploy different versions of their project to development, staging, or production environments.

Most popular use cases for firebase-tools:

  1. Firebase Tools can be used for initializing and configuring Firebase projects. By utilizing commands like firebase init, developers can easily set up their project, select the desired Firebase services, and generate the necessary configuration files. This allows for a smooth project setup process and ensures that the project is properly linked to the Firebase backend.
  1. Firebase Tools enables local development and testing. Developers can run their Firebase functions locally using the firebase emulators:start command. This sets up a local server to emulate Firebase services, allowing developers to test their functions and database interactions without making actual API calls to the Firebase servers. Additionally, the CLI tool offers features like Firestore data import and export, providing developers with the ability to manage data locally and perform backups or migrations easily.
  1. Firebase Tools offers seamless deployment capabilities. With commands such as firebase deploy, developers can easily deploy their Firebase functions, hosting content, and other resources to the Firebase servers. The tool handles the deployment process, including packaging the code, uploading it to the servers, and configuring the necessary infrastructure. It also provides options to target specific environments, allowing developers to deploy different versions of their project to development, staging, or production environments.
Share

It’s Really not that Complicated.

You can actually understand what’s going on inside your live applications.

Try Lightrun’s Playground

Lets Talk!

Looking for more information about Lightrun and debugging?
We’d love to hear from you!
Drop us a line and we’ll get back to you shortly.

By clicking Submit I agree to Lightrun’s Terms of Use.
Processing will be done in accordance to Lightrun’s Privacy Policy.