问题
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