22

When I see the compiled code by Babel, they do not seem to be equivalent. Actually, the former transforms to exports.A = A, which is not equivalent to module.exports = A (Maybe it is module.exports.A = A?)

So is there an ES6 style module.export =? Or the syntax remains still in ES6? Or that syntax is no more recommended in ES6?

0

3 Answers 3

19

You can use

export default class A {

}

Or

class A {

}

export default A;

Which will export as

exports["default"] = A;
module.exports = exports["default"];

There's an explanation why in the interop section here.

In order to encourage the use of CommonJS and ES6 modules, when exporting a default export with no other exports module.exports will be set in addition to exports["default"].

8

You can use the following in Node v6:

"use strict" 

class ClassName {
 // class code
}

module.exports = ClassName

Save the above file as ClassName.js

To import it in another file Test.js:

"use strict"
var ClassName= require('./ClassName.js');
var obj = new ClassName( Vars . . . );

For more Info:

Here's an article on exporting classes from modules in Node v6

2

babel6 is not supported export default A to modules.export=A

you should add a plugin

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.