Why is exports not displayed as an object in terminal while using Export Literals in node.js?

If exports is an object in node module object, why it isn’t shown as an object in the terminal (Command Prompt in windows 10)?

Here is the code in Name.js module :

    module.exports ="Hello World"; 
    console.log(module);

and what I see in ternianl is this :

Module {
  id: '.',
  path: 'D:\Projects\Node\Tutorial',
  exports: 'Hello World',
  parent: null,
  filename: 'D:\Projects\Node\Tutorial\names.js',
  loaded: false,
  children: [],
  paths: [
    'D:\Projects\Node\Tutorial\node_modules',
    'D:\Projects\Node\node_modules',
    'D:\Projects\node_modules',
    'D:\node_modules'
  ]
}

As you see exports exports: 'Hello World' isn’t an object and is a string.

Answer

You replaced the module.exports object with a string so when you then log it, you just see the string as the object is gone. So, this assignment:

module.exports ="Hello World"; 

takes the object that was in the exports property on the module object and replaces it with a string. So, now when you log module.exports, you just see the string that you replaced it with.

Perhaps you meant to do something like:

module.exports.greeting = "Hello World"; 

where you leave the object in place and add a property to it.

This is all just plain Javascript so exports is just a property of the module object. If you assign something else to that property, then it replaces the prior object with whatever you assigned to it.


It’s pretty much the same conceptually to this:

let myexports = { name: "Hi I'm an object" };
console.log(myexports);

myexports = "Hi I'm a string";    // replace object with a string
console.log(myexports);

In either case, you’re assigning a string into a variable/property and replacing the previous object with that string.