如何从一个文件导出变量,并在另一个文件上require()导出的变量? [英] How can I export a variable from one file and require() the exported variable on another file?
本文介绍了如何从一个文件导出变量,并在另一个文件上require()导出的变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的文件夹结构:
collegesapp --
|-- node_modules -- express
| -- connect
| -- jade
| -- passport
|-- routes -- routes.js
|-- views -- index.jade
| -- signin.jade
|-- app.js
|-- package.json
exports.serialized_user = "Alex"; //in app.js file
var serialized_user = require('../app.js').serialized_user; //in routes.js file
因此,当我将 console.log(serialized_user)
添加到我的 routes.js
文件并运行该应用程序时,我最终在控制台中看到未定义的内容,而不是亚历克斯".您认为我的问题是什么?
So, when I add console.log(serialized_user )
to my routes.js
file and run the application, I end up having seeing undefined in console instead of "Alex". What do you think is my problem?
推荐答案
在app.js文件中,使用
in app.js file, use
module.exports.serialized_user = "Alex";
或
module.exports={serialized_user: "Alex"};
代替
exports.serialized_user = "Alex";
这篇关于如何从一个文件导出变量,并在另一个文件上require()导出的变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文