Difference between export const foo, export default foo and module.exports = foo

后端 未结 3 961
感动是毒
感动是毒 2021-02-01 15:57

I am really confused about:

  1. export const foo
  2. export default foo
  3. module.exports = foo;

I kno

3条回答
  •  旧巷少年郎
    2021-02-01 16:22

    The export statement is used to export functions, objects or primitives from a given file (or module).


    Named Exports This is a named export in ES6 javascript

    export const foo
    

    which is imported like:

    import { foo } from 'path'
    

    Default Export This is a default export (This can be imported using any name)

    export default foo
    

    which is imported like so:

    import bar from 'path'
    

    This is commonjs export which is used in nodejs programs.

    module.exports = foo;

    which is imported like:

    var foo = require('path')
    

    For more details

提交回复
热议问题