[英]How can I export a string from a file and require() the exported string on another file in NODE.js?
[英]How can I export a variable from one file and require() the exported variable on another file?
我的文件夾結構:
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
文件中並運行該應用程序時,我最終在控制台中看到的是undefined而不是“ Alex”。 您認為我的問題是什么?
在app.js文件中,使用
module.exports.serialized_user = "Alex";
要么
module.exports={serialized_user: "Alex"};
代替
exports.serialized_user = "Alex";
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.