-
-
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(); |
[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)
If you need FreshBooks_Element
as well, you will need to do var FreshBooks_Element = require("./FreshBooks_Element");
.
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.
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
In Summary, here's the changes I needed to make:
- At the top of the class TimeEntry.js, added a require line for the dependency:
var FreshBooks_Element = require('./Element');
-
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; -
After the declaration of the function constructor in Element.js:
function FreshBooks_Element()
{
...
}
Added:
module.exports = FreshBooks_Element;
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;