K3nzie
K3nzie

Reputation: 465

Javascript (imported) Class is not defined, ReferenceError

I need advice once again, in my project I'm using NodeJS, Webpack with babel-loader and I've split it into different files.

I keep getting the error :

ReferenceError: PlayerManager is not defined

PlayerManager is a Class that I imported in my main file :

import PlayerManager from './game/modules/PlayerManager';
import Player from './game/modules/Player';
import Game from './game/modules/Game';

It's weird cause if I check my compiled code in firefox's debugger, the classes are all there in my bundle file.

This is the line of code triggering this error:

this.playerManager = new PlayerManager(this.ctx);

Which is inside my Class file's constructor, imported before (see above)

export default class Game {
    constructor() {
        this.canvas = document.getElementById("gameCanvas");
        this.ctx = this.canvas.getContext("2d");
        this.w = window.innerWidth;
        this.h = window.innerHeight;
        this.frames = 60; 
        this.resize();
        this.players = {};
        this.playerManager = new PlayerManager(this.ctx);
        ....
        ....

Now I call new Game() and error triggers...

What could cause the error?

Is using new Class inside another one's constructor wrong?

Upvotes: 1

Views: 6877

Answers (1)

tremby
tremby

Reputation: 10079

An import is only available in that particular file.

You need to import PlayerManager into the file where your Game class is defined.

Upvotes: 1

Related Questions