Skip to content

Instantly share code, notes, and snippets.

@mikermcneil
Created February 23, 2013 06:55
Show Gist options
  • Select an option

  • Save mikermcneil/5018757 to your computer and use it in GitHub Desktop.

Select an option

Save mikermcneil/5018757 to your computer and use it in GitHub Desktop.
How commonjs works in the context of node.js

Gabe if you module.expoerts = {} you can use that objects methods and properties whereever you require it?

Mike

// foo.js
module.exports = {
 baz: 'hi'
};

// bar.js
var foo = require('./foo.js');
foo.baz === 'hi';

That's the commonjs spec

Gabe and what about module.exports = function() {}

Mike node does a little bit extra exactly commonjs wouldn't let you but node will

// foo.js
module.exports = function () {
 return 'hi';
};

// bar.js
var foo = require('./foo.js');
foo() === 'hi';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment