Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

262
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda