Javascript 在nodejs中导出变量和函数
我试图在nodejs中将一组全局变量和函数从一个Javascript文件导出到另一个Javascript文件 从 节点js.include.jsJavascript 在nodejs中导出变量和函数,javascript,node.js,Javascript,Node.js,我试图在nodejs中将一组全局变量和函数从一个Javascript文件导出到另一个Javascript文件 从 节点js.include.js var GLOBAL_VARIABLE = 10; exports.GLOBAL_VARIABLE = GLOBAL_VARIABLE; module.exports = { add: function(a, b) { return a + b; } }; 进入测试节点js include.js: var in
var GLOBAL_VARIABLE = 10;
exports.GLOBAL_VARIABLE = GLOBAL_VARIABLE;
module.exports = {
add: function(a, b) {
return a + b;
}
};
进入测试节点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));
但是变量;我得到以下输出:
We have imported a global variable with value undefined
and added a constant value to it NaN
为什么不导出全局变量?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
};