开发者

Require() wrapper in node.js?

开发者 https://www.devze.com 2023-01-18 15:07 出处:网络
I try to include a .js file in my app with node require(), but get this error.Any idea? a.js : function a() {

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
开发者_JAVA技巧


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();
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号