ES6 - Convert from 'require' to 'import'

Ecmascript 6

Ecmascript 6 Problem Overview


If the alternative to:

var Foo = require('foo');

is:

import Foo from 'foo';

What is the alternative to:

var Bar = require('foo').batz

Could it be:

import {batz}  from 'foo' ?

Ecmascript 6 Solutions


Solution 1 - Ecmascript 6

Nearly. It does however depend on how you are exporting them.

  • named exports (export var batz = …):

      import {batz as Bar} from 'foo';
    
  • default-exported object (export default {batz: …};) - should not be used:

      import Foo from 'foo';
      var Bar = Foo.batz;
    

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser1177440View Question on Stackoverflow
Solution 1 - Ecmascript 6BergiView Answer on Stackoverflow