Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Type Error - Is Not a Constructor [duplicate]

Wanted to instantiate a module within ES6 javascript and transpile to ES5. I am setting up 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 = {};
  }
}

The other is index.js --

import { Track } from './track';

const track = new Track();
console.log(track);

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

like image 416
Yasir Avatar asked Oct 10 '16 18:10

Yasir


1 Answers

You're exporting Track as default, so you should use default import. Change

import { Track } from './track';

to

import Track from './track';

See What is "export default" in javascript?


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

like image 178
Michał Perłakowski Avatar answered Oct 29 '22 19:10

Michał Perłakowski