217

I'm trying to create a module that exports multiple ES6 classes. Let's say I have the following directory structure:

my/
└── module/
    ├── Foo.js
    ├── Bar.js
    └── index.js

Foo.js and Bar.js each export a default ES6 class:

// Foo.js
export default class Foo {
  // class definition
}

// Bar.js
export default class Bar {
  // class definition
}

I currently have my index.js set up like this:

import Foo from './Foo';
import Bar from './Bar';

export default {
  Foo,
  Bar,
}

However, I am unable to import. I want to be able to do this, but the classes aren't found:

import {Foo, Bar} from 'my/module';

What is the correct way to export multiple classes in an ES6 module?

vimfluencer
  • 3,106
  • 3
  • 17
  • 25
  • 8
    just use `export` without the default – webdeb Jul 12 '16 at 23:53
  • 3
    You can only have one `default` export. Imagine if someone tried to do `import SomeClass from 'my/module'`. This would automatically import the `default` module from that path. If you had multiple default exports there, how would it know which one to import? – Saad Jul 13 '16 at 07:42

7 Answers7

370

Try this in your code:

import Foo from './Foo';
import Bar from './Bar';

// without default
export {
  Foo,
  Bar,
}

Btw, you can also do it this way:

// bundle.js
export { default as Foo } from './Foo'
export { default as Bar } from './Bar'
export { default } from './Baz'

// and import somewhere..
import Baz, { Foo, Bar } from './bundle'

Using export

export const MyFunction = () => {}
export const MyFunction2 = () => {}

const Var = 1;
const Var2 = 2;

export {
   Var,
   Var2,
}


// Then import it this way
import {
  MyFunction,
  MyFunction2,
  Var,
  Var2,
} from './foo-bar-baz';

The difference with export default is that you can export something, and apply the name where you import it:

// export default
export default class UserClass {
  constructor() {}
};

// import it
import User from './user'
Bill Keller
  • 793
  • 7
  • 22
webdeb
  • 12,993
  • 5
  • 28
  • 44
  • I am getting an `Unexpected token` error when building `export Foo from './Foo'; export Bar from './Bar'` – inostia Jan 16 '18 at 20:42
  • @inostia note, this is ES6 syntax, you probably need "babel" to support it – webdeb Jan 17 '18 at 15:39
  • I am using babel. I got that error when compiling with webpack. I think I need to do something like `export { default as Foo } from './Foo';`. I've seen that elsewhere – inostia Jan 17 '18 at 22:27
  • @inostia I also am experiencing this, `export { default as Foo } from './Foo';` was required to actually export it. – echolocation Mar 20 '18 at 16:20
26

Hope this helps:

// Export (file name: my-functions.js)
export const MyFunction1 = () => {}
export const MyFunction2 = () => {}
export const MyFunction3 = () => {}

// if using `eslint` (airbnb) then you will see warning, so do this:
const MyFunction1 = () => {}
const MyFunction2 = () => {}
const MyFunction3 = () => {}

export {MyFunction1, MyFunction2, MyFunction3};

// Import
import * as myFns from "./my-functions";

myFns.MyFunction1();
myFns.MyFunction2();
myFns.MyFunction3();


// OR Import it as Destructured
import { MyFunction1, MyFunction2, MyFunction3 } from "./my-functions";

// AND you can use it like below with brackets (Parentheses) if it's a function 
// AND without brackets if it's not function (eg. variables, Objects or Arrays)  
MyFunction1();
MyFunction2();
Syed
  • 15,657
  • 13
  • 120
  • 154
9

@webdeb's answer didn't work for me, I hit an unexpected token error when compiling ES6 with Babel, doing named default exports.

This worked for me, however:

// Foo.js
export default Foo
...

// bundle.js
export { default as Foo } from './Foo'
export { default as Bar } from './Bar'
...

// and import somewhere..
import { Foo, Bar } from './bundle'
inostia
  • 7,777
  • 3
  • 30
  • 33
5
// export in index.js
export { default as Foo } from './Foo';
export { default as Bar } from './Bar';

// then import both
import { Foo, Bar } from 'my/module';
PaoPao
  • 71
  • 1
  • 1
5

For multiple classes in the same js file, extending Component from @wordpress/element, you can do that :

// classes.js
import { Component } from '@wordpress/element';

const Class1 = class extends Component {
}

const Class2 = class extends Component {
}

export { Class1, Class2 }

And import them in another js file :

import { Class1, Class2 } from './classes';
xavier bs
  • 1,341
  • 1
  • 12
  • 9
0

you can do. export{className, className and so on}

-5

For exporting the instances of the classes you can use this syntax:

// export index.js
const Foo = require('./my/module/foo');
const Bar = require('./my/module/bar');

module.exports = {
    Foo : new Foo(),
    Bar : new Bar()
};

// import and run method
const {Foo,Bar} = require('module_name');
Foo.test();
Schmidko
  • 690
  • 1
  • 7
  • 17