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?