Search code examples
javascriptecmascript-6requirejavascript-import

ES6 equivalent of require('module').function()


Here is my code

var foobar  = require('module').foobar()
foobar.useOne()
foobar.useTwo()

I am looking for the ES6 equivalent of the first line (with import ... as ... from) so that i can still do foobar.useOne(), foobar.useTwo() etc
If there is one..


Solution

  • Looks like your module exposes a factory method, so you need an intermediate variable to store the foobar instance.

    import { foobar as Foobar } from 'module';
    const foobar = Foobar();
    foobar.useOne();
    foobar.useTwo();