Type Error - Is Not a Constructor

Yasir picture Yasir · Oct 10, 2016 · Viewed 10k times · Source

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

Answer

Michał Perłakowski picture Michał Perłakowski · Oct 10, 2016

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).