Coding⏱️ 3 min read📅 2026-06-04

How to Fix: Extending Error in Javascript with ES6 syntax & Babel

ES6 syntax and Babel issue with extending Error object.

Quick Answer: The problem is that the `super` call in the constructor is not being executed correctly due to a change in how constructors work in ES6. Try using the `this` keyword instead of `super` to set the message property.

The error in extending Error with ES6 and Babel is caused by the incorrect use of the constructor function. In JavaScript, when you extend a class from another class using inheritance, you should call the parent class's constructor using the 'this' keyword or super(), not directly passing the message to the super() function.

This issue affects developers who are new to ES6 and Babel syntax, as well as those who have experience but are still learning about class inheritance in JavaScript.

🛑 Root Causes of the Error

  • The primary reason for this error is the incorrect use of the constructor function. Inheritance in JavaScript requires calling the parent class's constructor using super() or this.
  • An alternative cause could be that the Babel compiler version is outdated, which may not support ES6 syntax correctly.

✅ Best Solutions to Fix It

Correctly extending Error with ES6 and Babel

  1. Step 1: Use the 'super' keyword to call the parent class's constructor.
  2. Step 2: Pass the message as an argument to the super() function, like this: super(message).
  3. Step 3: Make sure you are using the correct syntax for your environment (e.g., Babel version) and compiler settings.

Verifying Babel compatibility with ES6 syntax

  1. Step 1: Check the Babel documentation to ensure that the version you are using supports ES6 syntax.
  2. Step 2: Verify your compiler settings to ensure that they are set to use the correct syntax and version of JavaScript.

💡 Conclusion

To resolve this error, follow the steps outlined in Method 1: Correctly extending Error with ES6 and Babel. Additionally, verify your Babel compatibility with ES6 syntax by following the steps in Method 2: Verifying Babel compatibility with ES6 syntax.

Did this fix your problem?

If not, try searching for specific error codes.

🔍 Search Error Database

❓ Frequently Asked Questions