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
@ 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?
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
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!