NodeJS - Requiring module returns empty array

node.js

Solution

The reason `exports` is not working is because of the reference conflict. The top variable in each file is `module` which has a property `module.exports`. When the module is loaded new variable is created in the background. Something like this happens:

var exports = module.exports;

Obviously `exports` is a reference to `module.exports`, but doing

exports = function(){};

forces `exports` variable to point at function object - it does not change `module.exports`. It's like doing:

var TEST = { foo: 1 };
var foo = TEST.foo;
foo = "bar";
console.log(TEST.foo);
// 1

Common practice is to do:

module.exports = exports = function() { ... };

I have no idea why it doesn't work under Windows Powershell. To be honest I'm not even sure what that is. :) Can't you just use native command prompt?

Problem

Writing the simplest module we could, we write into hello.js: ``` var hello = function(){ console.log('hello'); }; exports = hello; \\ Doesn't work on Amazon EC2 Ubuntu Instance nor Windows Powershell ``` I run Node and require the module ``` var hello = require('./hello'); hello; ``` and an empty array `{}` gets returned when I'm supposed to get `[Function]`. I tried replacing `exports` with `module.exports`, but this doesn't work on my Windows Powershell. It does work on my Amazon EC2 Ubuntu Instance, so why doesn't `exports` work? Has the API changed? And what could possibly be happening with Powershell that neither of these work? I know Windows isn't the most desirable development environment, but I can't get my head around such a simple mishap.

Original source