Skip to content

Instantly share code, notes, and snippets.

@damonsk
Created September 4, 2013 20:12
Show Gist options
  • Save damonsk/6442247 to your computer and use it in GitHub Desktop.
Save damonsk/6442247 to your computer and use it in GitHub Desktop.
// bah.js
module.exports.bah = function(){
function ponies(){
return "hai";
}
};
// test.js
var bah = require("./bah");
console.log(bah.ponies());
@damonsk
Copy link
Author

damonsk commented Sep 4, 2013

TypeError: Object # has no method 'ponies'
at Object. (C:\SourceCode\Bitbucket\test.js:3:
17)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
at startup (node.js:119:16)
at node.js:901:3

@RyanGough
Copy link

you're assigning the proprety "bah" on the exports object to be a function. That function contains a definition of a function called "ponies" but its not assigned to a property of bah so you can't access it in this way.

if you just want to export the ponies function, easiest way is to just do

module.exports.ponies = function(){return "hai";}

then you can do

var bah = require("./bah"); // bah is assigned the "module.exports" object from bah.js file which has ponies property
console.log(bah.ponies());

@RyanGough
Copy link

a good way to sanity check this stuff is to fire up the node repl and do require("./bah.js") and it will show you what is being exported

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment