Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

257
Views
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 answers
Answer question

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 Report

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!