Below code is review:

var conf = {
   index: {
      path: {
         first: "index.html",
         pattern: "index/{num}.html"
      },
      template: "index.tpl",
      limit: 8
   },
   feed: {
      path: "feed.xml",
      template: "atom.tpl",
      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 e; // process.nextTick error, or 'error' event on first tick
        ^
SyntaxError: Unexpected identifier
    at Module._compile (module.js:397:25)
    at Object..js (module.js:408:10)
    at Module.load (module.js:334:31)
    at Function._load (module.js:293:12)
    at require (module.js:346:19)
    at Object.<anonymous> (/home/paul/dev/indexing/lib/Index.js:3:13)
    at Module._compile (module.js:402:26)
    at Object..js (module.js:408:10)
    at Module.load (module.js:334:31)
    at Function._load (module.js:293:12)

Why Did It Go Wrong?

The error message SyntaxError: Unexpected identifier means the computer didn’t understand the instruction for each. It’s like trying to speak a word that isn’t in the computer’s dictionary.

The problem is that for each…in is a special kind of instruction that most modern JavaScript, including Node.js (which is what the user is running their code with), doesn’t understand or support. It was a feature mostly found in older web browsers like Firefox, but it never became a standard rule for all JavaScript.

So, when the user’s Node.js program saw for each, it was confused because that’s not a valid command in its language.

The Correct Way to Do It

Option 1: Using for…in Loop (The Standard Way)

The for…in loop is the most traditional and widely supported way to iterate over the enumerable properties of an object.

for (var key in conf) {
  // Check if the 'key' actually belongs to 'conf'
  // and isn't something inherited from its "parent" types.
  if (conf.hasOwnProperty(key)) {
    console.log(conf[key].path);
  }
}

Option 2: Use Object.keys().forEach() (The Modern Standard)

This is a more modern and often preferred approach when you simply want to iterate over an object’s own enumerable property values. Object.keys() was introduced in ECMAScript 5, so it’s generally well-supported even in older Node.js versions (like v0.4.11).

Object.keys(conf).forEach(function(key) {
  console.log(conf[key].path);
});

Option 3: Using Object.entries() with for…of (The Modern Node.js)

This is excellent for current Node.js versions, it’s worth noting that it won’t work with very old Node.js versions like v0.4.11 as Object.entries() and for…of were introduced in newer JavaScript standards (ES2017 and ES2015, respectively).

// This works in modern Node.js versions (NOT v0.4.11)
for (const [key, value] of Object.entries(conf)) {
  console.log(value.path);
}

Recommendation

For your specific scenario with Node.js v0.4.11, the for…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.

For any new projects or when using a modern Node.js environment, Object.keys().forEach() or even Object.entries() with for…of are generally preferred for their clarity and modern syntax.

Also Read

Also Read:

Node JS Multer

Need Help With Node Development?

Work with our skilled Node developers to accelerate your project and boost its performance.

Hire Node.js Developers

Support On Demand!

Related Q&A