I have an ES6 class defined in an ES6 module that exports an instance of that class:
class MyObject {
  constructor() {
    this.propertyA = 1;
    this.propertyB = 2;
  }
  myMethod() {
    doStuff();
  }
}
var theInstance = new MyObject();
export default theInstance;
When I import this module, myMethod is undefined:
import * as theObject from './my/module';
theObject.myMethod(); // Error! undefined is not a method.
The properties defined in the constructor do exist. It's as though the object's prototype was excluded, and only its members were exported.
I am requiring 'babel/register'.
Why is exporting this object not working correctly?
I figured this out right after asking. It looks like there's a difference between import * as foo from 'module' and import foo from 'module'. This works:
import theObject from './mymodule';
So it wasn't a matter of the wrong thing being exported, but it was being imported incorrectly.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With