Is ES6 `export class A` equivalent to `module.exports = A`?

前端 未结 3 1131
终归单人心
终归单人心 2020-12-28 16:42

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 mod

相关标签:
3条回答
  • 2020-12-28 17:13

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

    you should add a plugin

    0 讨论(0)
  • 2020-12-28 17:19

    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

    0 讨论(0)
  • 2020-12-28 17:28

    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"].

    0 讨论(0)
提交回复
热议问题