[英]Exporting variables and functions in nodejs
I'm trying to export a set of global variables and functions from one Javascript file to another in nodejs. 我正在尝试将一组全局变量和函数从一个Javascript文件导出到nodejs中的另一个。
From node-js.include.js 来自node-js.include.js
var GLOBAL_VARIABLE = 10;
exports.GLOBAL_VARIABLE = GLOBAL_VARIABLE;
module.exports = {
add: function(a, b) {
return a + b;
}
};
into test-node-js-include.js : 进入test-node-js-include.js :
var includes = require('./node-js-include');
process.stdout.write("We have imported a global variable with value " + includes.GLOBAL_VARIABLE);
process.stdout.write("\n and added a constant value to it " + includes.add(includes.GLOBAL_VARIABLE, 10));
but the variable; 但变量; I get the following output: 我得到以下输出:
We have imported a global variable with value undefined
and added a constant value to it NaN
why isn't GLOBAL_VARIABLE exported? 为什么不出口GLOBAL_VARIABLE ?
2 ways to fix this: 2种解决方法:
module.exports.GLOBAL_VARIABLE = GLOBAL_VARIABLE;
module.exports.add: function(a, b) {
return a + b;
};
module.exports.GLOBAL_VARIABLE = GLOBAL_VARIABLE;
module.exports = {
add: function(a, b) {
return a + b;
},
GLOBAL_VARIABLE: GLOBAL_VARIABLE
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.