JVerstry JVerstry - 1 month ago 5x
Javascript Question

How to export a ES6 class properly in Node 4?

I define a class in a module:

"use strict";

var AspectTypeModule = function() {};
module.exports = AspectTypeModule;

var AspectType = class AspectType {
// ...

module.export.AspectType = AspectType;

But I get the following error message:

TypeError: Cannot set property 'AspectType' of undefined
at Object.<anonymous> (...\AspectType.js:30:26)
at Module._compile (module.js:434:26)

How should I export this class and use it in another module? I have seen other SO questions, but I get other error messages when I try to implement their solutions.


If you are using ES6 in Node 4, you cannot use ES6 module syntax without a transpiler, but CommonJS modules (Node's standard modules) work the same.


should be


hence the error message "Cannot set property 'AspectType' of undefined" because module.export === undefined.

Also, for

var AspectType = class AspectType {
    // ...    

can you just write

class AspectType {
    // ...    

and get essentially the same behavior.