-1

If your objective here is Detecting an undefined object property, then go check that question.

If you are perplexed why the JavaScript engine says a variable is undefined when you are trying to access one of its properties, then you probably did the same silly mistake as I did. Read on and check the accepted answer.


I have the following bit of code which produces an error:

File lib.js:

var Lib;

(function() {
    var X = "X";        
    Lib.X = X;
})();

module.exports = Lib;

When this is run on command line:

$ node lib.js

Node.js produces following error:

lib.js:4
    Lib.X = X;
       ^
TypeError: Cannot set property 'X' of undefined
    at <path>\lib.js:4:8
    at Object.<anonymous> (<path>\lib.js:
16:2)
    at Module._compile (module.js:446:26)
    at Object..js (module.js:464:10)
    at Module.load (module.js:353:31)
    at Function._load (module.js:311:12)
    at Array.0 (module.js:484:10)
    at EventEmitter._tickCallback (node.js:190:38)

I can see that the problem is in the statement Lib.X = X;. But I am not sure if that line breaks any syntax/semantic rules. I understand this line as: assign function X to property X of variable Lib.

What am I doing wrong?

sampathsris
  • 21,564
  • 12
  • 71
  • 98
  • @downvoter: Care to explain why? Not really interested about reps. But does this show no research effort? Or is it unclear? Or useless? :D – sampathsris Oct 15 '14 at 06:05
  • I didn't downvote, but this does seem to be an on-topic, clear question - even if it was a simple overlooked issue. – Brendan Oct 15 '14 at 06:08
  • 2
    Possible duplicate of [Detecting an undefined object property](https://stackoverflow.com/questions/27509/detecting-an-undefined-object-property) – Liam Nov 20 '18 at 12:04
  • @Liam: Not really. Yes, no need to keep this open, but this has nothing to do with the linked question. This is a silly mistake and should be categorized as typographical error (If I remember correctly there's a closing category for that, but I cannot find it in close votes). – sampathsris Nov 20 '18 at 15:18
  • I'm voting to close this question as off-topic because this is a silly mistake, and thus should be closed. This has nothing to do with the linked (supposedly duplicate) question. – sampathsris Nov 20 '18 at 15:19
  • This appears in search results that ultimately should be directed at the dupe target, as that gives better advice than this Q&A. – Liam Nov 20 '18 at 15:19
  • See https://www.google.co.uk/search?q=cannot+read+property+%27x%27+of+undefined+site%3Astackoverflow.com&rlz=1C1CHBF_en-GBGB785GB785&oq=cannot+read+property+%27x%27+of+undefined+site%3Astackoverflow.com&aqs=chrome.0.69i59.12500j0j9&sourceid=chrome&ie=UTF-8 – Liam Nov 20 '18 at 15:19
  • @Liam: Yeah OK fine I also voted to close. – sampathsris Nov 20 '18 at 15:20

2 Answers2

4

Lib is undefined. undefined is not an object, therefore you can't set any properties on it. You probably want this at the start of the file:

var Lib = {};
simonzack
  • 19,729
  • 13
  • 73
  • 118
3

Variable Lib is undefined, so you can't assign anything to it.

You need to define it first, as a minimal example:

var Lib = {};
Brendan
  • 2,777
  • 1
  • 18
  • 33