Skip to content

Instantly share code, notes, and snippets.

@janjongboom
Created February 22, 2012 13:52
Show Gist options
  • Save janjongboom/1885247 to your computer and use it in GitHub Desktop.
Save janjongboom/1885247 to your computer and use it in GitHub Desktop.
function MyObject () {
// do stuff
}
// now to export this as a nodejs module do
module.exports = MyObject;
// =====
// in another file (in the same folder) you can now do
var MyObject = require("./MyObject");
// use it just like before
var obj = new MyObject();
@mrjcleaver
Copy link

and with var timeEntry = require('freshbooks-javascript-library/library/FreshBooks/TimeEntry');
and in TimeEntry.js var Element = require('./Element');
FreshBooks_TimeEntry.prototype = new FreshBooks_Element();
and in Element.js function FreshBooks_Element()
{
this.elementName = "";
this.lastError = "";
}

module.exports = FreshBooks_Element;

@mrjcleaver
Copy link

[2012-02-22 08:53:33.821] [DEBUG] [default] - App Starting

node.js:201
throw e; // process.nextTick error, or 'error' event on first tick
^
ReferenceError: FreshBooks_Element is not defined
at Object. (/Volumes/Storage/martincleaver/SoftwareDevelopment/freshbooks-javascript-library/library/FreshBooks/TimeEntry.js:2:38)

@janjongboom
Copy link
Author

If you need FreshBooks_Element as well, you will need to do var FreshBooks_Element = require("./FreshBooks_Element");.

@mrjcleaver
Copy link

According to my base source code, the definition of FreshBooks_Element is in FreshBooks/Element.js rather than in FreshBooks_Element.js
( i.e. https://github.com/mrjcleaver/freshbooks-javascript-library/blob/master/library/FreshBooks/Element.js )

Must I rename the file to exactly match the class name?

(I'm back on IRC)

Thanks, M.

@mrjcleaver
Copy link

MartinCleaver: Does Node's Require mandate that filenames match classnames? (I am trying to minimally adapt a base javascript library where class FreshBooks_Element comes from file FreshBooks/Element.js)
[10:18am] bradleymeck: MartinCleaver, no
[10:19am] konobi: MartinCleaver: anyways... no... require... look at the "module" docs
[10:20am] konobi: http://nodejs.org/docs/latest/api/modules.html
[10:20am] MartinCleaver: I have http://nodejs.org/api/modules.html#loading_from_node_modules_Folders open
[10:21am] • MartinCleaver is searching for the module naming rules… I think I've seen it in the past
[10:21am] bradleymeck: there is none MartinCleaver, the only thing node cares about is file extensions
[10:21am] konobi: modules are just objects... they can be named anything you want
[10:21am] bradleymeck: you can export anything you want

@mrjcleaver
Copy link

In Summary, here's the changes I needed to make:

  1. At the top of the class TimeEntry.js, added a require line for the dependency:

var FreshBooks_Element = require('./Element');

  1. After the declaration of the function constructor in TimeEntry.js:
    function FreshBooks_TimeEntry()
    {
    this.elementName = "time_entry";
    ...
    }
    Added, per http://nodejs.org/api/modules.html#module.exports and http://stackoverflow.com/questions/8296379/create-a-class-in-nodejs:
    module.exports = FreshBooks_TimeEntry;

  2. After the declaration of the function constructor in Element.js:

function FreshBooks_Element()
{
...
}

Added:
module.exports = FreshBooks_Element;

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