7

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

1
  • 2
    import Track from './track' Commented Oct 10, 2016 at 18:57

2 Answers 2

12

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

Sign up to request clarification or add additional context in comments.

Comments

6

The problem is with the way you're importing Track in 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}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.