Named export and default export in the same file
Asked Answered
K

2

53

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 ?

Kaycekaycee answered 17/10, 2016 at 12:42 Comment(0)
K
74

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}
Kaycekaycee answered 21/10, 2016 at 12:5 Comment(3)
export {a as default, b as c, d, e, f};Celom
As of July 2022 this isn't a good option according to Vite bundler. Any other ideas?Splashboard
@КонстантинВан: You mean export {a as default, a as b, c, d, e};, right? OP is wanting to export same function twice under different guises :) export {fetchUtil as default, fetchUtil as fetch, post, put, get}Mariehamn
C
3

If you're just creating an index file you can just re-export the default and the named seperately

export { default } from "./your-file";
export * from "./your-file";
Cetinje answered 20/4, 2022 at 11:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.