Solution 1 :

try this: import Paddle from "./paddle.js";
u main problem was that u don’t put “.”

Problem :

I am trying to import the class Paddle from the paddle.js file but it keeps getting an error of “Error: SyntaxError: Cannot use import statement outside a module”. The files are in the same folder.

Here’s my import code (from game.js file):

import Paddle from "/paddle.js";
let canvas = document.getElementById("screen");
let ctx = canvas.getContext("2d");

const game_width = 800;
const game_height = 600;

ctx.clearRect(0, 0, 800, 600);

and here’s my code from paddle.js:

export default class Paddle {
constructor(gameWidth, gameHeight){

    this.width = 150;
    this.height = 30;

    this.position = {

        x: gameWidth / 2 - this.width / 2,
        y: gameHeight - this.height - 10,

    }
}

draw(ctx){
    ctx.fillRect(this.position.x, this.position.y, this.width, this.height);
}

update(deltaTime){

    if(!deltaTime) return;
    this.position.x += 5 / deltaTime;
}

}

Comments

Comment posted by Teemu

The script element has to be type of module, i.e. set

Comment posted by Andy

The class is being exported as

Comment posted by Brenz

Tried that because I was desperate but it doesn’t work class is being exported as default.

Comment posted by Alex

@Brenz check edited answer

Comment posted by Brenz

@Alex its still not working. That really is all there is in my code nothing else.

By