tags:

views:

41

answers:

1

I try to include a .js file in my app with node require(), but get this error. Any idea?

a.js :

function a() {
  this.a = 'a';
}

Node application :

require(./a.js);
var test = new a();

Error:

/Users/.../app.js:14
var test = new a()
^
ReferenceError: a is not defined
+2  A: 

Read about commonjs modules here (or just follow examples below): http://wiki.commonjs.org/wiki/Modules/1.0

a.js should be:

function a() {
  this.a = 'a';
}

exports.a = a; //this exports a

your app should be:

var everything_in_module_a = require('./a.js');
var a = everything_in_module_a.a;
var test = new a();

or your app could be:

var a = require('./a.js').a;
var test = new a();
z5h