Search code examples
javascriptecmascript-6exportrequirees6-module-loader

es6 equivalent for module.exports


What's the ES6 equivalent for module.exports

I want to get the value of foo from an import statement

module.exports = {
    foo: function (a) {
    }
}

Tried:

export default {
    foo: function (a) {
    }
}

The way first one is imported is using:

var file;
var filename = root + "/" + fileStats.name;
file = require(path.resolve(filename));

I want to use ES6 import statement. I read somewhere that this isn't supported however would like to still know if there's a work around this.


Solution

  • Not sure what you're trying to do because in the code you supplied you did not consume the actual foo method from the object you imported.

    But if I understand correctly, you could achieve this in one of 2 ways:

    export default function foo(a) { };
    

    and consume the module with:

    import foo from './<filename>.js';
    

    Or alternatively, don't use the default export:

    export function foo(a) {};
    

    and consume with:

    import { foo } from './<filename>.js';