Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

502
Vistas
How to use import and export functions using reactjs?

I am new-learning React library and have this problem for two weeks now cant solve it. so when i have all the Functions in one file "Index.js"

function Header(){
    return(
        <header>
            <nav className="nav">
                <img src="./logo-react.png" width="100px"/>
                <ul className="nav-items">
                    <li>Price</li>
                    <li>About</li>
                    <li>Contact</li>
                </ul>
            </nav>
        </header>
    )
}

function Maincont(){
    return (
        <div>
            <Header />
        </div>
    )
}

ReactDOM.render(< Maincont />,document.getElementById("root"))

it all works good no problem, but once I cut on of the Functions to other file "Header.js" and try to export-import it to the "index.js" it stops working I am using React with CDN links and this is my HTML Code as well

<script crossorigin src="https://unpkg.com/react@17/umd/react.development.js"></script>
<script crossorigin src="https://unpkg.com/react-dom@17/umd/react-dom.development.js"></script>
<script src="https://unpkg.com/babel-standalone@6/babel.min.js"></script>
<script src="index.js" type="text/babel"></script>

I have also tried to change the type to Module but still no chance this is what shows me in console

index.js:3 Uncaught ReferenceError: require is not defined
    at <anonymous>:3:15
    at i (babel.min.js:24:29679)
    at r (babel.min.js:24:30188)
    at e.src.n.<computed>.l.content (babel.min.js:24:30503)
    at XMLHttpRequest.n.onreadystatechange (babel.min.js:24:29946)
(anonymous) @ index.js:3
i @ babel.min.js:24
r @ babel.min.js:24
e.src.n.<computed>.l.content @ babel.min.js:24
n.onreadystatechange @ babel.min.js:24
XMLHttpRequest.send (async)
s @ babel.min.js:24
(anonymous) @ babel.min.js:24
o @ babel.min.js:24
u @ babel.min.js:24
f @ babel.min.js:1
(anonymous) @ babel.min.js:1
 
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Babel doesn't polyfill require. You need to either:

Use ES6 modules where your entry point is loaded with <script type="module"> and then you use import and export instead of require and module.exports. I believe you will also need to make some changes to Babel to get it to handle ES6 module imports client-side.

Transpile and bundle your code before sending it to browser using a tool like Webpack or Parcel in combination with Babel.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda