0
votes

I am new of using node.js and I get these error :

TypeError: Cannot set property 'size' of undefined at Object. (C:\Users\User\Documents\latihannode.js\javascript4\test\script 4.js:4:10) at Module._compile (internal/modules/cjs/loader.js:678:30) at Object.Module._extensions..js (internal/modules/cjs/loader.js:689:10) at Module.load (internal/modules/cjs/loader.js:589:32) at tryModuleLoad (internal/modules/cjs/loader.js:528:12) at Function.Module._load (internal/modules/cjs/loader.js:520:3) at Function.Module.runMain (internal/modules/cjs/loader.js:719:10) at startup (internal/bootstrap/node.js:228:19) at bootstrapNodeJSCore (internal/bootstrap/node.js:576:3)

in the notification this is the error code:

die.size = 10;

this is the code in page script4.js:

var dice = require('./dice');
var die = dice.die;

    die.size = 10;
    console.log(die.roll());
    console.log(die.roll());
    console.log(die.roll());
    console.log(die.totalRolls);

    console.log(die);

and this is in page dice.js:

var die = {
  size: 6,
  totalRolls: 0,
  roll: function() {
    var result = Math.ceil(this.size * Math.random());
    this.totalRolls += 1;
    return result;
  }
};


exports.game = die;
exports.name = "My dice exports";
1
Have you checked if dice is null or not? - lucumt
The problem is that die is undefined, just as it says in the error message. Does dice have a die export? - user9315861
Your naming seems to be contradictory: you're trying to access something you don't export-might want to get that figured out before going much further. - Dave Newton
i already give export die in dice.js but i think is in require @torazaburo - M Romi Muhtarom
So many dice problems today. Guess a new semester started and everyone from the class is asking questions. The error is clearly stating that dice.die; is undefined. Look at your exports, do you see die? No. I see game and name. Basic debugging var dice = require('./dice'); console.log(dice); <-- look to see what dice outputs to your developer console. - epascarello

1 Answers

2
votes

Change here :

var dice = require('./dice');
var die = dice.game; // not die.die