Parsing error only one default export allowed per module

This multiple component doesn't work; import React from 'react'; import ReactDOM from 'react-dom'; import { Router, Route, Link, browserHistory, IndexRoute } from 'react-router' class App extends

This multiple component doesn’t work;

import React from 'react';
import ReactDOM from 'react-dom';
import { Router, Route, Link, browserHistory, IndexRoute  } from 'react-router'

class App extends React.Component {
   render() {
      return (
         <div>
            <ul>
               <li><Link>Home</Link></li>
            </ul>

           {this.props.children}
         </div>
      )
   }
}

export default App;

class Home extends React.Component {
   render() {
      return (
         <div>
            <h1>Home...</h1>
         </div>
      )
   }
}

export default Home;

ReactDOM.render((
   <Router history = {browserHistory}>
      <Route path = "/" component = {App}>
         <IndexRoute component = {Home} />
         <Route path = "home" component = {Home} />
      </Route>
   </Router>

), document.getElementById('app'))

It give a below error;

ERROR in ./main.js Module build failed: SyntaxError:
C:/Users/hasithay/Desktop/reactApp/main.js: Only one default export
allowed per module.

31 | } 32 |

33 | export default Home;
| ^ 34 | 35 | class About extends React.Component { 36 | render() {

@ multi main webpack: bundle is now VALID

Answer should be three clickable links that can be used to change the route When the app is started.

tenten's user avatar

tenten

1,2762 gold badges26 silver badges53 bronze badges

asked Sep 7, 2016 at 4:35

Hasitha Yapa's user avatar

1

Multiple component does work but you need to export the component with name and you can only export one default component.

Like in below expample I export the App Component as defalut component and other component Home, About, Contact as a named component.

For the named component you need to import them using there name :

import {Home,About,Contact} from ‘./App.jsx’;

import default component:

import App from ‘./App.jsx’;

import React from 'react';
import {Link} from 'react-router';


class App extends React.Component {
  render() {
    return(
      <div>
      <ul>
      <li><Link to="home">Home</Link></li>
      <li><Link to="about">About</Link></li>
      <li><Link to="contact">Contact</Link></li>
      </ul>

      {this.props.children}
      </div>
    )
  }
}

export default App;



export class Home extends React.Component {
  render() {
    return (
      <h1>Home Page Content</h1>
    )
  }
}



export class About extends React.Component {
  render() {
    return (
      <h1>About Page Content</h1>
    )
  }
}


export class Contact extends React.Component {
  render()  {
    return (
      <h1>Contact Page Content</h1>
    )
  }
}
import React from 'react';
import ReactDOM from 'react-dom';
import {Router, Route, browserHistory, IndexRoute} from 'react-router';
import App from './App.jsx';
import {Home,About,Contact} from './App.jsx';

ReactDOM.render((
   <Router history = {browserHistory}>
      <Route path = "/" component = {App}>
         <IndexRoute component = {Contact} />
         <Route path = "home" component = {Home} />
         <Route path = "about" component = {About} />
         <Route path = "contact" component = {Contact} />
      </Route>
   </Router>

	
), document.getElementById('app'));

Don’t import default component (App Component) with the name component (Home, About, Contact). if you import them with the named component they didn’t render properly.

Blockquote

answered Sep 28, 2016 at 12:55

webx's user avatar

webxwebx

9577 silver badges11 bronze badges

1

import React from 'react';
import {Link} from 'react-router';


class App extends React.Component {
  render() {
    return(
      <div>
      <ul>
      <li><Link to="home">Home</Link></li>
      <li><Link to="about">About</Link></li>
      <li><Link to="contact">Contact</Link></li>
      </ul>

      {this.props.children}
      </div>
    )
  }
}

export default App;



export class Home extends React.Component {
  render() {
    return (
      <h1>Home Page Content</h1>
    )
  }
}



export class About extends React.Component {
  render() {
    return (
      <h1>About Page Content</h1>
    )
  }
}


export class Contact extends React.Component {
  render()  {
    return (
      <h1>Contact Page Content</h1>
    )
  }
}

answered Nov 4, 2016 at 8:04

Rohit Kumar's user avatar

you have one line with the code:

export default App;

After some other lines you have the code:

export default Home;

That’s just the problem! you have used export default 2 times in one file. you have to change one of them to solve the problem.

«you cannot use export default more than one time in a file».

answered Feb 28, 2018 at 12:29

Mahdieh Shavandi's user avatar

export all the components in one line

export default {App, Home, Contacts, About};

answered Nov 11, 2018 at 4:57

Anmol Middha's user avatar

Export Default Home is used to Expose any module to use in other files, but only one component is a default not all. A module can only be exported once. You are using the same statement to export each component which is unnecessary.

Importing components using this statement

import {Home,About,Contact} from './App.jsx';

MaartenDev's user avatar

MaartenDev

5,4265 gold badges20 silver badges32 bronze badges

answered Sep 1, 2019 at 9:23

Muhammad Talha's user avatar

You need to remove default keywords on both App and Home classes, as per code below:

export App;
export Home;

default keywords is only use when you want to export ONE class.

answered Sep 16, 2018 at 15:16

Jerry Chong's user avatar

Jerry ChongJerry Chong

7,2304 gold badges42 silver badges38 bronze badges

This article will tell you the reason for the “Only one default export allowed per module” in React error and two methods to fix it like removing the export default or using the class component method. Let’s go into detail now.

When does the error “Only one default export allowed per module” in React happen?

There is only one default export per module. Export default can be a function, a class, an object, or anything else. This value is considered the main export value because it is the simplest to import.

Code:

import React from 'react';
 
const App = () => {
   return (
    <>
    <h2>Only one default export allowed per module in React | LearnShareIT</h2>
    </>
  )
}
 
export default App;
const Home = () => {
  return (
    <>
     <h2>This is Home component</h2>
    </>
  );
};
 
export default Home;

Output:

This error will appear when you export default more than once in a component, webpack will recognize it and give you a warning.

How to fix this error?

Removing the export default method

As described in the warning, each module in React can only have one default export, so we will remove the default export from your module that is failing.

Code:

import React from 'react';
 
const App = () => {
   return (
    <>
    <h2>Only one default export allowed per module in React | LearnShareIT</h2>
    <Home/>
    </>
  )
}
 
export default App;
export const Home = () => {
  return (
    <>
     <h2>This is Home component</h2>
    </>
  );
};

Output:

Once you removed the export default from the component, you were able to import the Home component into the App component and render it to the screen without the “Only one default export allowed per module” error.

Using the export class method

ES6 classes make up class components. In addition to having, they have: a life-cycle, the render() function, the constructor, and state (data) management.

A component of a React class is:

  • An ES6 class, it will be a part when it “acquires” Respond part.
  • Can acquire props if required (in the constructor).
  • Can keep its data and state together
  • A render() method that returns a React element (JSX) or null is required.

Code:

import React from 'react';
class App extends React.Component {
  render() {
    return(
      <>
      <h2>Only one default export allowed per module in React | LearnShareIT</h2>
      <hr/>
      <Home/>
      <About/>
      </>
    )
  }
}
 
export default App;
 
 class Home extends React.Component {
  render() {
    return (
      <h3>This is Home class</h3>
    )
  }
}
class About extends React.Component {
  render() {
    return (
      <h3>This is About class</h3>
    )
  }
}

Output:

We can use export class Name extends React.Component to create the components we need to export and can avoid the warning “only one default export allowed per module” in React. Wish you success with the above methods.

Summary

To summarize, we have known what causes the “Only one default export allowed per module” in React and how we can fix it. We hope you like this article.

Maybe you are interested:

  • Unexpected default export of anonymous function
  • Does not contain a default export Error in React.js
  • Export ‘usehistory’ was not found in react-router-dom

Nathaniel Kirk

Hi, my name’s Nathaniel Kirk. I’m interested in learning and sharing knowledge about programming languages. My strengths are C, C++, Python, HTML, CSS, Javascript, and Reactjs. If you have difficulty with them, don’t worry. I’m here to support you.


Name of the university: PTIT
Major: IT
Programming Languages: C, C++, Python, HTML, CSS, Javascript, Reactjs

@tarim

Hi there,

I updated my dependencies, after update, I got error messages. Here is details. Need help.

Thanks,

ERROR in ./src/components/details.js
Module build failed: SyntaxError: C:/src/components/details.js: Only one default export allowed per module.

export default connect(mapStateToProps,api)(Details);
export default reduxForm({
form: Details’,
fields,
validate
},mapStateToProps,api)(Details);

@iyn

It’s not a problem with redux-form. As error message states, you can only have 1 default export in the file (more info: https://developer.mozilla.org/en/docs/web/javascript/reference/statements/export and http://exploringjs.com/es6/ch_modules.html). If you use redux-form v6 (if you use versions below v6 please ignore this sentence), I can also see another problem with the code you posted — reduxForm API is different now, as it only takes configuration object and component but you pass mapStateToProps and api variables. Also, with v6, redux connect need to come after reduxForm.

Assuming that you’re using v6, you can do something like this:

export const DetailsForm = reduxForm({
// your redux-form config
})(Details);

export default connect(mapStateToProps){DetailsForm);

The default export here is component decorated with reduxForm and redux connect, but redux-form decorated component is also exported and can be imported somewhere else (e.g for tests) like this: import { DetailsForm } from '(component path'.

Does this solve your issue?

@gustavohenke

As @iyn kindly answered, I’ll close this one 😄

@lock

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@lock
lock
bot

locked as resolved and limited conversation to collaborators

Jun 2, 2018

I got an error when i tried to compile multiple modules on one page. Error is: «Parsing error: Only one default export allowed per module.

import React from «react»;

import { Doughnut } from «react-chartjs-2»;

import { MDBContainer } from «mdbreact»;

export class ChartsPage extends React.Component {

state = {

dataDoughnut: {

labels: [«Red», «Green», «Yellow», «Grey», «Dark Grey»],

datasets: [

{

data: [300, 50, 100, 40, 120],

backgroundColor: [«#F7464A», «#46BFBD», «#FDB45C», «#949FB1», «#4D5360»],

hoverBackgroundColor: [

«#FF5A5E»,

«#5AD3D1»,

«#FFC870»,

«#A8B3C5»,

«#616774»

]

}

]

}

}

render() {

return (

<MDBContainer>

<h3 className=»mt-5″>Radar chart</h3>

<Doughnut data={this.state.dataDoughnut} options={{ responsive: true }} />

</MDBContainer>

);

}

}

export default ChartsPage;

import React, { Component } from «react»;

import { Carousel, CarouselCaption, CarouselInner, CarouselItem, View, Mask } from «mdbreact»;

class CarouselPage extends Component {

render() {

return (

<Carousel activeItem={1} length={4} showControls={true} showIndicators={true} className=»z-depth-1″>

<CarouselInner>

<CarouselItem itemId=»1″>

<View>

<img className=»d-block w-100″ src=»https://mdbootstrap.com/img/Photos/Slides/img%20(68).jpg» alt=»First slide» />

<Mask overlay=»black-light» />

</View>

<CarouselCaption>

<h3 className=»h3-responsive»>Light mask</h3>

<p>First text</p>

</CarouselCaption>

</CarouselItem>

<CarouselItem itemId=»2″>

<View>

<img className=»d-block w-100″ src=»https://mdbootstrap.com/img/Photos/Slides/img%20(99).jpg» alt=»Second slide» />

<Mask overlay=»black-strong» />

</View>

<CarouselCaption>

<h3 className=»h3-responsive»>Strong mask</h3>

<p>Second text</p>

</CarouselCaption>

</CarouselItem>

<CarouselItem itemId=»3″>

<View>

<img className=»d-block w-100″ src=»https://mdbootstrap.com/img/Photos/Slides/img%20(17).jpg» alt=»Third slide» />

<Mask overlay=»black-slight» />

</View>

<CarouselCaption>

<h3 className=»h3-responsive»>Slight mask</h3>

<p>Third text</p>

</CarouselCaption>

</CarouselItem>

<CarouselItem itemId=»4″>

<View>

<img className=»d-block w-100″ src=»https://mdbootstrap.com/img/Photos/Slides/img%20%28143%29.jpg» alt=»Mattonit’s item» />

<Mask overlay=»black-light» />

</View>

<CarouselCaption>

<h3 className=»h3-responsive»>Sopot Beach</h3>

<p>Taken june 21th by @mattonit</p>

</CarouselCaption>

</CarouselItem>

</CarouselInner>

</Carousel>

);

}

}

export default CarouselPage;

Понравилась статья? Поделить с друзьями:

Читайте также:

  • Parsing error loading custom advancement
  • Partition table error 105 found как исправить
  • Parsing error loading built in advancement
  • Partition table entries are not in disk order как исправить
  • Parsing error legacy octal literals are not allowed in strict mode

  • 0 0 голоса
    Рейтинг статьи
    Подписаться
    Уведомить о
    guest

    0 комментариев
    Старые
    Новые Популярные
    Межтекстовые Отзывы
    Посмотреть все комментарии