Type error - not a constructor

It is required to create an instance of the module in javascript ES6 and upgrade to ES5. I am creating a new class in my project which is es6 / webpack. I have 2 files: track.js, which has the following -

export default class Track { constructor() { this.o = {}; } } 

Another is index.js -

 import { Track } from './track'; const track = new Track(); console.log(track); 

I am trying to make the console log show an empty object. Instead, I get - Uncaught TypeError: _track.Track is not a constructor

+5
source share
2 answers

The problem is how you import Track into index.js . You need to either import, like this:

 import Track from './track'; 

Or in track.js you need to export it like this:

 export {Track} 
+5
source

You export Track by default, so you should use the default import. The change

 import { Track } from './track'; 

to

 import Track from './track'; 

See What is “default export” in javascript?


When you execute import { Track } from './track' , you are trying to access the Track property of the exported object (which is the Track class), which is undefined (therefore, it is not a constructor).

+8
source

Source: https://habr.com/ru/post/1257979/


All Articles