0

so I'm trying to keep my code clean. To do so I used VS code's built-in code refactoring "move to a new file". As always it couldn't be that easy so everything is not working. When I'm trying to use this method in my Game class, I get an error saying getRandomInt is not a function. I've seen a dozen StackOverflow threads about this, although none of them could fix my problem. Then I thought it may be just an individual problem, so here I am. Stuck with the same problem for two days...

Game.js:


const { getRandomInt } = require("./index.js");

class Game {
    /**
     *
     * @param {Discord.Message} message
     * Passes the message object for the playerId that will be the game Id
     */
    constructor(message) {
        this.cards = [2, 3, 4, 5, 6, 7, 8, 9, 10, 11];
        this.playerId = message.author.id;
        this.dealerCards = [];
        this.playerCards = [];
        this.message = message;
    }

    start() {
        console.log(this.cards);
        for (let index = 0; index < 2; index++) {
            this.playerCards.push(this.cards[getRandomInt(1, this.cards.length)]); 
            //There's an error saying that getRandomInt is not a function
            this.dealerCards.push(this.cards[getRandomInt(1, this.cards.length)]);

        }
        console.log(this.playerCards);
        console.log(this.dealerCards);
        this.message.channel.send(`Dealer cards: ${this.dealerCards[0]} ${this.dealerCards[1]}\nPlayer cards ${this.playerCards[0]} ${this.playerCards[1]}`);

    }

    hit() {
        this.playerCards.push(this.cards[getRandomInt(1, this.cards.length)]);

    }
}
exports.Game = Game;

index.js (a bit cut down, but it shouldn't make a difference):

const { Game } = require("./Game.js");

function getRandomInt(min, max) {
    min = Math.ceil(min);
    max = Math.floor(max);
    return Math.floor(Math.random() * (max - min)) + min;
}
exports.getRandomInt = getRandomInt;

Is it that hard? It's built into VS code's refactoring system, so I think something is on me.

kalempster
  • 397
  • 3
  • 13
  • Your title says `exports.getRandomInt = getRandomInt` which would be correct, your file says `module.exports = new getRandomInt;` which is wrong. – tkausl Nov 28 '20 at 15:12
  • Oh, my bad. I was trying out many things and none of them worked out, I'll change that so it doesn't get anyone confused. But it doesn't work anyway – kalempster Nov 28 '20 at 15:18

1 Answers1

3

Circular dependency issue. I don't know why you need to import Game.js in your index.js file but removing it will fix the problem. If you still want to use Game.js in your index.js, there're some approaches for you:

  • Make an empty module export. Ex: put exports.Game = function(){}; and exports.getRandomInt = function(){}; at the beginning of modules. It depends on your logic that this approach can fix your problem, you better provide more detail about your code to know your real issue.
  • Refactor your code.

More info:

How to deal with cyclic dependencies in Node.js

https://nodejs.org/api/modules.html#modules_cycles

Hoang Dao
  • 775
  • 5
  • 16
  • Well I didn't. VS code's refactoring system did. All I did was that I clicked on the Game class, then refactoring, and then move to a new file, it wasn't working. Could you explain it on my example? – kalempster Nov 28 '20 at 19:30
  • [index.js](https://pastebin.com/xQts1y2B) and [Game.js](https://pastebin.com/U33NZJ6X) – kalempster Nov 28 '20 at 19:37
  • if you don't know what it is, or what it does to your code, why did you use it? – Hoang Dao Nov 28 '20 at 19:40
  • Well hell, I do know what it does, I wrote it myself. The only copied thing is the getRandomInt function, which I was too lazy to write. That shouldn't be the problem though. And I haven't said that I don't know what my code does. And the reason is that I wanted for it to look... "slick?". Just it's prettier when it isn't in one giantic file (made that mistake before) – kalempster Nov 28 '20 at 19:42
  • 1
    okay, keep it straight: keep the line `const { Game } = require("./Game.js");` as is, move the rest of code in `index.js` to another file (such as `getRandomInt.js`), change the `require` file location in `Game.js` to point to the new file. – Hoang Dao Nov 28 '20 at 19:47
  • It works! I moved the getRandomInt to another file and somehow everything started working haha. Thank you I've been stuck at this for 2 days. – kalempster Nov 28 '20 at 19:58