Let's say I have a module I want to re-export:
//exportme.js
export default 'EXPORTME';
export const test = () => console.log('test function');
//reexport.js
export * from './exportme.js'
When I import reexport.js, the default from exportme.js is not available.
//app.js
import reexport from './reexport.js'
console.log(reexport) //undefined
I have to make reexport.js to be the following for it to work.
export * from './exportme.js'
export default from './exportme.js'
Is there an easier way to do this or can this be consolidated into one statement?
export default, * from './exportme.js'
does not work.
I am using latest babel with transform-export-extensions
ES6 provides two ways to export a module from a file: named export and default export. With named exports, one can have multiple named exports per file. Then import the specific exports they want surrounded in braces. The name of imported module has to be the same as the name of the exported module.
An ES6 module can pick a default export, the main exported value. Default exports are especially easy to import.
To re-export values from another file in JavaScript, make sure to export the name exports as export {myFunction, myConstant} from './another-file. js and the default export as export {default} from './another-file. js' . The values can be imported from the file that re-exported them.
Every module can have two different types of export, named export and default export. You can have multiple named exports per module but only one default export.
The default from
exportme.js
is not available
Yes, default exports are not re-exported by star exports. The purpose of export * from …
is to allow re-exports from multiple modules, exporting the default
from multiple modules would only lead to collisions. You therefore have to specifiy it explicitly (if you need it at all, often there is no default export alongside named exports).
Is there an easier way to do this or can this be consolidated into one statement?
No, the two lines you have are the way to go.
As Bergi wrote, there is no way to do this in one line using ES 6 exports. You can, however, simply require the module you want to re-export and assign the result to module.exports
:
module.exports = require('./exportme.js')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With