How can I export a variable from one file and require() the exported variable on another file?

主宰稳场 提交于 2019-12-11 14:47:29

问题


my folder structure :

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

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?


回答1:


in app.js file, use

module.exports.serialized_user = "Alex";

or

module.exports={serialized_user: "Alex"};

instead of

exports.serialized_user = "Alex";


来源:https://stackoverflow.com/questions/26871996/how-can-i-export-a-variable-from-one-file-and-require-the-exported-variable-on

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!