ES6, how can you export an imported module in a single line?
Asked Answered
B

7

155

I'd like to the following but with a single line, if possible:

  • import Module from './Module/Module;'
  • export Module;

I tried the following but it doesn't seem to work:

  • export Module from './Module/Module;
Becalm answered 27/2, 2016 at 4:42 Comment(1)
P
288
export {default as Module} from './Module/Module';

is the standard ES6 way, as long as you don't need Module to also be available inside the module doing the exporting.

export Module from './Module/Module';

is a proposed ESnext way to do it, but that only works if you've enabled it in Babel for now.

Paschasia answered 27/2, 2016 at 5:5 Comment(9)
It worked great, however, it seems like Webpack doesn't like this, giving a notification that the component is now read-only and unable to be hot reloaded. Very strange!Becalm
perfect, this should be the accepted answer. (if webpack hot reload doesn't like that's a problem in that tool or it's HMR plugin.)Cheryl
If anyone is wondering which babel plugin it is, it is export-extensions here - babeljs.io/docs/plugins/transform-export-extensionsFrancklin
@Paschasia is there a way to export the above as default?Gastrostomy
@abhishek-kdm export { default as default } from or export { default } fromPaschasia
Problem seems to be if Module is commonjs module with only default export and you do not want to use it inside current module with this statement, but still want to exported.. I have utilities module which is reexporting some methods from lodash, but they are commonjs modules, only option seems to be using lodash-es npm package :/Bennington
Also I have just found that using export {default as Module} from './Module/Module'; Module is not defined in current file 🤷‍♂️Bennington
What's the best way to also use that export in the same file? Edit: just have duplicate lines with import & export I guess.Gimcrack
@Gimcrack Yes you'd want to separate them.Paschasia
R
45

I don't know why but just this works for me :

components/index.js:

import Component from './Component';
import Component2 from './Component2';
import Component3 from './Component3';
import Component4 from './Component4';

export {Component, Component2, Component3, Component4};

I import the exports like this :

import {Component, Component2, Component3, Component4} from '../components';
Reflectance answered 11/7, 2017 at 10:59 Comment(2)
⚠ this method kills three shakingBennington
How can this be adjusted to not kill tree shaking?Tenebrous
M
38

Please note you can also re-export everything from a module:

export * from './Module/Module';
Monika answered 7/10, 2018 at 13:28 Comment(1)
This wildcard syntax will only work on files with named exports. If the file only has a single default export, you'll get the error "No named exports found in module".Loyceloyd
L
21

For React Native components this syntax works for me:

export {default} from 'react-native-swiper';
Luculent answered 16/1, 2019 at 13:18 Comment(1)
This works for me for React (not Native) when I wish to re-export an imported default. I use this in index.js files that don't apply any HOCs to my 'pure' componentsDishonest
M
0
// Service
// ...
export const paymentService = new PaymentService()

// Entry services/index.js file
export * from './paymentService'

// use it like
import { paymentService } from './services/'
Mapes answered 31/10, 2020 at 21:21 Comment(0)
L
0
  export {Module as default} from '/path'

that worked for me with Module as named exported.

export {default} from '/path'

if Module is exported as default from the file where defined

Lather answered 19/4 at 5:32 Comment(0)
B
-3

So, I've found this to work quite well for the immediate export functionality of having an index.js at the root of the components directory for easy referencing:

import Component from './Component/Component'
import ComponentTwo from './ComponentTwo/ComponentTwo'

module.exports = {
  Component,
  ComponentTwo
};

You need to use module.exports.

Becalm answered 9/6, 2016 at 14:39 Comment(5)
Keep in mind that since this is partially CommonJS modules, this will only work specifically in Babel and will fail if you attempt to use it in a real ES6 module once support for them lands in more environments, and will likely stop working in future versions of Babel.Paschasia
Correct. Intermingling commonJS & es6 import / export in Babel 6 breaks. Babel5 allowed / reinforced this incorrect behavior. In your example, Component will no longer be a reference to your exported component, but instead will be an object, with your instance reference living on Component.defaultGreig
Anybody know how to do this without using module.exports ? I like this method of packaging a bunch of components into an index.js but can't figure out the syntax. import x from 'x'; import y from 'y'; export default {x, y}; then import {x} from xy; doesn't work (and I can't figure out why not)Depressor
Alex, did you try export {x, y} instead?Podvin
This answer is not es6. It's a non-EcamScript platform. -1Phyfe

© 2022 - 2024 — McMap. All rights reserved.