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

258
Vistas
How to impoort js type code into another js

I have 3 files: index.html, index.js, Sprite.js

I want to import Sprite class from Sprite.js into index.js

I've tryed this:

    import Sprite from "Sprite.js"

And also this:

    import Sprite from Sprite

When i open the html with Google Chrome it gives me this error:

Uncaugth SyntaxError: Cannot use import statement outside a module
about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

@ Juan Pablo Arano There might be a problem when you want to use javascript modules out of modules context. The is almost no chance to achieve that without Webpack or npm. If you want to use a module out of a modules context, you need to use above compilators before you run the code.

The following can be useful:

How do I include a JavaScript file in another JavaScript file?

about 4 years ago · Juan Pablo Isaza Denunciar

0

You have to initially export your function to be able to import it in another file. It'll be very useful for every project, try to learn about it:
MDN export documentation

about 4 years ago · Juan Pablo Isaza Denunciar

0

After seeing your updated question with the error "Uncaugth SyntaxError: Cannot use import statement outside a module", you can use module.exports always for example, Sprite.js file will look like the following,

const Sprite = () => {
  console.log("Sprite function")
}

module.exports = Sprite;

Inside index.js file, you can just do,

const Sprite = require("./Sprite")

// your code goes here!

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