Node.js module.exports assignment resulting in method undefined error

To make mongoose setup easier, I've created a mongoose.js file in my /lib directory. Its setting up mongoose so consuming modules can have the connection ready etc. The problem is, when trying to access the mongoose.Schema method on product.js, it throws method undefined. In doing a similar practice for my own objects, this does not happen. Here is the code in question.

/lib/mongoose.js (lib file)

var mongoose = require('mongoose');
mongoose.connect(process.env.PPC_API_MONGODB_URL);

var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function callback () {
  console.log('db open');
});

exports = mongoose;

/models/product.js (consuming file)

var mongoose = require('../lib/mongoose.js');

var productSchema = mongoose.Schema({ // results in method undefined here
    name: String
});

exports = mongoose.model('Product', productSchema);

I can get this to work if I do the following in /lib/mongoose.js

exports.mongoose = mongoose;

Then within /models/product.js

var mongoose = require('mongoose').mongoose;

but this is not clean and I'm simply not understanding why the first option doesn't work.

If you could either show me what I'm doing wrong or explain why the first approach does not work, that would be considered a checked off answer with my up vote.

You can't reassign the exports variable, but instead you can use module.exports

module.exports = mongoose;

If you're wondering why, what you have in each module is essentially this:

var module = { exports: {} };
var exports = module.exports;

So if you add properties onto exports, you can still access those through module.exports

exports.test = function () { console.log('test'); };
module.exports.test(); // outputs 'test'

But if you reassign exports, then module.exports and exports point to different things.

var module = { exports: {} };
var exports = module.exports;

exports = { test: 'test' };

console.log(exports);        // { test: 'test' }
console.log(module.exports); // {}

When require() looks for what to include, it uses module.exports.