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 mand 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._pile (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?
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 mand 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._pile (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?
Share Improve this question edited Nov 20, 2018 at 15:38 sampathsris asked Oct 15, 2014 at 5:58 sampathsrissampathsris 22.3k12 gold badges72 silver badges101 bronze badges 8- @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 Commented Oct 15, 2014 at 6: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 Commented Oct 15, 2014 at 6:08
- 2 Possible duplicate of Detecting an undefined object property – Liam Commented Nov 20, 2018 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 Commented Nov 20, 2018 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 Commented Nov 20, 2018 at 15:19
2 Answers
Reset to default 4Lib
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 = {};
Variable Lib
is undefined, so you can't assign anything to it.
You need to define it first, as a minimal example:
var Lib = {};