Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Named export and default export in the same file

I am trying to put default and named export in same file. Example:

// file name : utils/fetch export default fetchUtil;  module.exports = {     fetch : fetchUtil,     post,     put,     get, };  // import code import fetch from 'utils/fetch'; 

My code builds fine with webpack, however in browser I get errors :

fetchInit.js:27 Uncaught TypeError: (0 , _fetch2.default) is not a function

Am I missing something or is this not the way to do default & named import in the same file ?

like image 662
Umang Gupta Avatar asked Oct 17 '16 12:10

Umang Gupta


People also ask

Can I export and export default in same file?

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.

What is the difference between default export and named export?

Exports without a default tag are Named exports. Exports with the default tag are Default exports. Using one over the other can have effects on your code readability, file structure, and component organization. Named and Default exports are not React-centric ideas.

Can you have two export default?

The only natural restriction to multiple exports is the default export. In each file, there can be only one default export function.

What are named exports?

What are Named Exports? Named exports allow us to share multiple objects, functions or variables from a single file and were introduced with the release of ES2015. Named exports are imported with curly braces in various files and must be imported using the name of the object, function or variable that was exported.


1 Answers

Found the solution here : http://exploringjs.com/es6/ch_modules.html

Basically, I had to do

export default fetchUtil export {fetchUtil as fetch, post, put, get} 
like image 170
Umang Gupta Avatar answered Sep 21 '22 22:09

Umang Gupta