0

I'm trying to import a JS class from the file "paddle.js" to be used in another file "game.js" (no JS libraries or frameworks used, just pure simple VanillaJS), and I can't seem to figure why this import/export is not not working. I have seen too many examples and tutorials of this, and I'm doing exactly as they say, but it just does'nt work. It might be a silly question, but any help is appreciated.

This is from a YT tutorial that I'm following, and I'm basically just going with the approach of adding "export default class Paddle" at the top of paddle.js, and including an import statement like "import Paddle from './paddle' ", in game.js. And then instantiating that class in game.js file. But it doesn't work. I've already tried all combinations like:

import Paddle from 'paddle'
import Paddle from './paddle'
import Paddle from '/paddle'
import Paddle from 'paddle.js'
import Paddle from './paddle.js'

None of them work.

// File: paddle.js
export default class Paddle{
  constructor(gameWidth, gameHeight){
     // some code
  }

  draw(ctx){
    // a function in this class
  }
}


// file: game.js

import Paddle from './paddle.js';
//var Paddle = require('paddle'); this doesn't work either


let c = document.getElementById("gameScreen");
let ctx = c.getContext("2d");

ctx.fillRect(20, 20, 100, 100); // would print a rectangle as a test

const GAME_WIDTH = 800;
const GAME_HEIGHT= 600;

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

let paddle = new Paddle(GAME_WIDTH, GAME_HEIGHT); // instantiating Paddle

paddle.draw(ctx);

// end

It is expected to successfully instantiate the paddle class, and draw a paddle on my html5 canvas. But instead, the inclusion of the import statement in the beginning causes the file to not function at all, i.e. my canvas is empty.

edit(as asked by some viewers): // Here is my js_game.html

<!DOCTYPE html>
<html lang="en">
    <head>
      <meta charset="utf-8">
      <title>VanillaJS game</title>
      <link rel="stylesheet" type="text/css" href="game.css">
    </head>
    <body>
      <canvas id="gameScreen" width="800" height="600">

      </canvas>
      <script src="game.js" type="text/javascript"></script>
      <!-- <script src="paddle.js"></script> -->

    </body>
</html>
singh.rbir
  • 85
  • 1
  • 8
  • What is your folder structure? Can you paste it here? – llievredemars Jul 09 '19 at 17:21
  • 2
    did you put type='module' in the link to paddle.js ? – Scott Weaver Jul 09 '19 at 17:29
  • Are you getting any errors? How do you include the `game.js` in your website? What is your project structure? – Bergi Jul 09 '19 at 17:40
  • @llievredemars I was just making sure that folders weren't causing any disputes, so I just transferred every all files under the same folder. And removed VanillaJS data path from the import statement. So I'm certain that's not causing any problems. – singh.rbir Jul 09 '19 at 17:58
  • @ScottWeaver No I didn't. And sorry I'm not sure how would I go about doing that. – singh.rbir Jul 09 '19 at 18:00
  • @Bergi No I'm not getting any errors. I'm just using plain html, css, JS in Atom code editor. And outputting the results in browser. Also, project structure is medieval. All files are under the same folder. I removed the Vanilla_JS folder path from import statement to avoid discrepancies. – singh.rbir Jul 09 '19 at 18:03
  • As Scott said, please show us the part of your html where you include the script. – Bergi Jul 09 '19 at 18:09
  • @Bergi I just included my game.html file in the post. Also, I added the script line in the body of the file because like I said, I'm following a youtube tutorial and this is how he did it, and it worked for him. And when I added the script line the head element, it didn't work. – singh.rbir Jul 09 '19 at 18:17
  • As @ScottWeaver already said, you're missing the `type=module` attribute. No idea why you wouldn't get an error in your browser devtools console. – Bergi Jul 09 '19 at 18:23
  • 1
    duplicate of https://stackoverflow.com/questions/37624819/es2015-import-doesnt-work-even-at-top-level-in-firefox and/or https://stackoverflow.com/questions/41722621/es6-in-the-browser-uncaught-syntaxerror-unexpected-token-import – Bergi Jul 09 '19 at 18:24
  • @Bergi so I added the type="module" and it still doesn't work. Does this mean there's no way to include a class from another JS file into a JS file, to be used and instantiated in the latter (in Vanilla JS)? – singh.rbir Jul 09 '19 at 18:34
  • It should work. If you're not getting any error message, and it just mysteriously "doesn't work", I'm afraid we can't really help you. – Bergi Jul 09 '19 at 18:37
  • @Bergi I'm sorry where can I check errors? I'm just using a text editor and browser. – singh.rbir Jul 09 '19 at 18:48
  • @programmerSingh https://webmasters.stackexchange.com/q/8525 – Bergi Jul 09 '19 at 18:49
  • @Bergi I checked the console for errors. This is what I'm getting: "SyntaxError: Unexpected identifier 'Paddle'. import call expects exactly one argument." Which is weird because Paddle is the only argument in my import statement. – singh.rbir Jul 09 '19 at 18:59
  • Can you post the exact contents of `game.js`? Is the `import` declaration on the top level? Are you sure there are no parenthesis around? – Bergi Jul 09 '19 at 19:05
  • @Bergi plz check the post again. I added the whole content of the game.js file – singh.rbir Jul 09 '19 at 19:18

1 Answers1

2

I think that it's because you use ES6 syntax, but probably don't have Babel to transpile it for you. In this case, if you don't want to configure Babel (although you should use ES6, it's newer), you should export your Paddle using module.exports = Paddle and then in your game.js you should use var Paddle = require('./paddle')

llievredemars
  • 170
  • 2
  • 15
  • I just tried that and it doesn't work either. I'm just surprised why these plain and simple things are not working. Unless I just include the class code of paddle in the game.js file itself. It would work that way but It's just gonna be a mess of a code. – singh.rbir Jul 09 '19 at 18:02
  • There is no `module.exports` or `require` in a browser environment – Bergi Jul 09 '19 at 18:20
  • @Bergi since `module.exports` does not exist in a browser env, do you have a proposed solution? I am curious myself on how to make this work. Thank you. – CodeConnoisseur Apr 12 '21 at 13:46
  • @PA-GW See the duplicates for how to use native browser support for ES6 modules. Alternatively, use a module loader library and a transpiler. – Bergi Apr 12 '21 at 14:51