{"id":13323,"date":"2025-09-08T11:30:54","date_gmt":"2025-09-08T11:30:54","guid":{"rendered":"https:\/\/www.bacancytechnology.com\/qanda\/?p=13323"},"modified":"2025-09-08T11:32:42","modified_gmt":"2025-09-08T11:32:42","slug":"for-each-in-not-working","status":"publish","type":"post","link":"https:\/\/www.bacancytechnology.com\/qanda\/node\/for-each-in-not-working","title":{"rendered":"Node.js: for each \u2026 in Not Working"},"content":{"rendered":"<p>Below code is review:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"typescript\">var conf = {\r\n   index: {\r\n      path: {\r\n         first: \"index.html\",\r\n         pattern: \"index\/{num}.html\"\r\n      },\r\n      template: \"index.tpl\",\r\n      limit: 8\r\n   },\r\n   feed: {\r\n      path: \"feed.xml\",\r\n      template: \"atom.tpl\",\r\n      limit: 8\r\n   }\r\n}\r\n\r\nfor each (var index in conf) {\r\n  console.log(index.path);\r\n}\r\n<\/pre>\n<p><strong><br \/>\nGetting the following error:<\/strong><\/p>\n<pre class=\"lang:markup-templating\">for each (var index in conf) {\r\n     ^^^^\r\n\r\nnode.js:134\r\n        throw e; \/\/ process.nextTick error, or 'error' event on first tick\r\n        ^\r\nSyntaxError: Unexpected identifier\r\n    at Module._compile (module.js:397:25)\r\n    at Object..js (module.js:408:10)\r\n    at Module.load (module.js:334:31)\r\n    at Function._load (module.js:293:12)\r\n    at require (module.js:346:19)\r\n    at Object.&lt;anonymous&gt; (\/home\/paul\/dev\/indexing\/lib\/Index.js:3:13)\r\n    at Module._compile (module.js:402:26)\r\n    at Object..js (module.js:408:10)\r\n    at Module.load (module.js:334:31)\r\n    at Function._load (module.js:293:12)<\/pre>\n<h2>Why Did It Go Wrong?<\/h2>\n<p>The error message SyntaxError: Unexpected identifier means the computer didn&#8217;t understand the instruction for each. It&#8217;s like trying to speak a word that isn&#8217;t in the computer&#8217;s dictionary.<\/p>\n<p>The problem is that for each&#8230;in is a <strong>special kind of instruction that most modern JavaScript, including Node.js (which is what the user is running their code with), doesn&#8217;t understand or support.<\/strong> It was a feature mostly found in older web browsers like Firefox, but it never became a standard rule for all JavaScript.<\/p>\n<p>So, when the user&#8217;s Node.js program saw for each, it was confused because that&#8217;s not a valid command in its language.<\/p>\n<h2>The Correct Way to Do It<\/h2>\n<h3>Option 1: Using for&#8230;in Loop (The Standard Way)<\/h3>\n<p>The for&#8230;in loop is the most traditional and widely supported way to iterate over the <strong>enumerable properties <\/strong>of an object.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"typescript\">\r\nfor (var key in conf) {\r\n  \/\/ Check if the 'key' actually belongs to 'conf'\r\n  \/\/ and isn't something inherited from its \"parent\" types.\r\n  if (conf.hasOwnProperty(key)) {\r\n    console.log(conf[key].path);\r\n  }\r\n}\r\n<\/pre>\n<h3>Option 2: Use Object.keys().forEach() (The Modern Standard)<\/h3>\n<p>This is a more modern and often preferred approach when you simply want to iterate over an object&#8217;s own enumerable property values. Object.keys() was introduced in ECMAScript 5, so it&#8217;s generally well-supported even in older Node.js versions (like v0.4.11).<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"typescript\">\r\nObject.keys(conf).forEach(function(key) {\r\n  console.log(conf[key].path);\r\n});\r\n<\/pre>\n<h3>Option 3: Using Object.entries() with for&#8230;of (The Modern Node.js)<\/h3>\n<p>This is excellent for current Node.js versions, it&#8217;s worth noting that it won&#8217;t work with very old Node.js versions like v0.4.11 as Object.entries() and for&#8230;of were introduced in newer JavaScript standards (ES2017 and ES2015, respectively).<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"typescript\">\r\n\/\/ This works in modern Node.js versions (NOT v0.4.11)\r\nfor (const [key, value] of Object.entries(conf)) {\r\n  console.log(value.path);\r\n}\r\n<\/pre>\n<h2>Recommendation<\/h2>\n<p>For your specific scenario with Node.js v0.4.11, the for&#8230;in loop with the hasOwnProperty check or Object.keys().forEach() are your go-to solutions. They are fully compatible and reliable for older Node.js versions.<\/p>\n<p>For any new projects or when using a modern Node.js environment, Object.keys().forEach() or even Object.entries() with for&#8230;of are generally preferred for their clarity and modern syntax.<\/p>\n<div class=\"qanda-read-box\"><div class=\"bg-light read-more-icon\"><img decoding=\"async\" src=\"https:\/\/assets.bacancytechnology.com\/qanda\/wp-content\/uploads\/2025\/04\/24061434\/read-txt.png\" alt=\"Also Read\"><p><\/p><h3>Also Read:<\/h3><a href=\"https:\/\/www.bacancytechnology.com\/blog\/node-js-multer\" target=\"_blank\">Node JS Multer<\/a><\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Below code is review: var conf = { index: { path: { first: &#8220;index.html&#8221;, pattern: &#8220;index\/{num}.html&#8221; }, template: &#8220;index.tpl&#8221;, limit: 8 }, feed: { path: &#8220;feed.xml&#8221;, template: &#8220;atom.tpl&#8221;, limit: 8 } } for each (var index in conf) { console.log(index.path); } Getting the following error: for each (var index in conf) { ^^^^ node.js:134 throw [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":13324,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[8],"tags":[],"class_list":["post-13323","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-node"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts\/13323"}],"collection":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/comments?post=13323"}],"version-history":[{"count":2,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts\/13323\/revisions"}],"predecessor-version":[{"id":13326,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts\/13323\/revisions\/13326"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/media\/13324"}],"wp:attachment":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/media?parent=13323"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/categories?post=13323"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/tags?post=13323"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}