如何从一个文件导出变量并将()导出的变量导出到另一个文件?

时间:2023-01-07 23:21:10

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?

因此,当我将console.log(serialized_user)添加到我的routes.js文件并运行应用程序时,我最终在控制台中看到未定义而不是“Alex”。你觉得我的问题是什么?

1 个解决方案

#1


3  

in app.js file, use

在app.js文件中,使用

module.exports.serialized_user = "Alex";

or

要么

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

instead of

代替

exports.serialized_user = "Alex";

#1


3  

in app.js file, use

在app.js文件中,使用

module.exports.serialized_user = "Alex";

or

要么

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

instead of

代替

exports.serialized_user = "Alex";